locked
How to upload image to server on HTTP POST without namespace of System? RRS feed

  • Question

  • Hello

    I am developing Windows Store App now.

    I need to upload image file to Azure Server as Multipart-Content.

    I converted each data (fx, header , image file) to byte[] data.

    And I combined each byte[] data into one.

    here is my code,but it don't work.

    async private void ProfileImage_doubleTapped(object sender, DoubleTappedRoutedEventArgs e)
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                
                openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                
                openPicker.ViewMode = PickerViewMode.Thumbnail;
                
                openPicker.FileTypeFilter.Clear();
                openPicker.FileTypeFilter.Add(".bmp");
                openPicker.FileTypeFilter.Add(".png");
                openPicker.FileTypeFilter.Add(".jpeg");
                
                StorageFile file = await openPicker.PickSingleFileAsync();
                fileName = file.Name;
                
                if(file != null)
                {
                    
                    filestream = await file.OpenAsync(FileAccessMode.Read);
                    
                    bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(filestream);
                    
                    if(bitmapImage.PixelHeight >64 && bitmapImage.PixelWidth > 64)
                    {
                        return;
                    }
                    profileimage.Source = bitmapImage;
    
                    Uri targetAdresse = new Uri("http://synapse-server.cloudapp.net/Set/Profile.aspx");
                    
                    
                    HttpCookie cookie = new HttpCookie("sid", domainValue, "");
                    cookie.Value = sidValue;
                    cookie.Secure = false;
                    cookie.HttpOnly = false;
                    HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
                    var replaced = filter.CookieManager.SetCookie(cookie, false);
    
                    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                    byte[] boundaryByte = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
                    BasicProperties bp = await file.GetBasicPropertiesAsync();
                    string formDataTemplate = "name = \"{0}\"\r\n\r\n{1}";
                    string headerTemplate = "name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
                    string header = string.Format(headerTemplate, "prfimg", fileName, file.ContentType);
                    string formItem = string.Format(formDataTemplate, "prfimg", file);
                    byte[] fdata = new byte[bp.Size];
                    byte[] headerByte = System.Text.Encoding.UTF8.GetBytes(header);
                    byte[] formItemByte = System.Text.Encoding.UTF8.GetBytes(formItem);
                    byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
    
                    IInputStream istream = filestream.GetInputStreamAt(0);
                    DataReader reader = new DataReader(istream);
                    await reader.LoadAsync((uint)fdata.Length);
                    reader.ReadBytes(fdata);
                    
    
                    byte[] data = new byte[headerByte.Length + fdata.Length];
                    Array.Copy(headerByte, 0, data,0, headerByte.Length);
                    Array.Copy(fdata, 0, data,headerByte.Length, fdata.Length);
    
                    MemoryStream mem = new MemoryStream(data);
                    IInputStream rstream = mem.AsInputStream();
    
    
                    HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, targetAdresse);
                    HttpMultipartFormDataContent content = new HttpMultipartFormDataContent(boundary);
                    HttpStreamContent stream = new HttpStreamContent(rstream);
                    content.Add(stream);
                    requestMessage.Content = content;
    
                    HttpResponseMessage response;
                    HttpClient client = new HttpClient();
                    response = await client.SendRequestAsync(requestMessage);
                    profileText.Text = await response.Content.ReadAsStringAsync();
                }
                  
            }

    I want some advice.



    Sunday, February 2, 2014 4:27 PM

All replies

  • What specifically doesn't work? How does the expected behaviour differ from the actual behaviour?

    See Connecting to an HTTP server using Windows.Web.Http.HttpClient (Windows Store apps using C#/VB/C++ and XAML)

    Sunday, February 2, 2014 4:31 PM
    Moderator
  • I think correct data in HttpMultipartFormDataContent is like this

    -----------------------------******
    Content-Dis-data

    name=*****; filename=*****
    Content-Type: ******

    (binary of image)

    ------------------------------*******------

    but my code shows

    -----------------------------8d0ef0afc678376
    Content-Disprfimg"; filename="kuro.png"
    Content-Type: image/png

    ‰PNG
    

    ‰I think it is cause that my code don't work

    


    Monday, February 3, 2014 6:29 AM
  • And why not just use the existing HttpMultipartContent class?  It's explicitly designed to handle the multipart form protocol with some really simple code.

    The network poster pack includes an HttpClient poster; the third column of sample code includes a Multipart content code snippet.


    Network Developer Experience Team (Microsoft)

    Wednesday, February 5, 2014 10:01 PM