Window Phone 8 submit post form with an image

只谈情不闲聊 提交于 2020-01-06 01:29:38

问题


I am working on window phone 8.0 and I have a problem when trying to submit a post request from mobile phone to a website. The input named "file" and it accepts only image file.

<form action="upmeme" method="post" enctype="multipart/form-data">
   <input type="file" class="file" name="file" id="file"><br>
   <input type="submit" class="submit" name="submit" value="Submit">
</form>

or you can visite here: this website

I used PhotoPicker to choose the photo from libary and keep it into "photo" Stream photo = e.ChosenPhoto; and it worked perfectly.

Now I need to upload the photo and submit the form above .This is my code called to send a post request, but it does not work, the response is the same as before summiting

photo.Position = 0;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);           
HttpRequestMessage request = new HttpRequestMessage();  
MultipartFormDataContent form = new MultipartFormDataContent(); 
form.Add(new StreamContent(photo),"file");
HttpResponseMessage response = await client.PostAsync(url, form); 
string responseBodyAsText = await response.Content.ReadAsStringAsync();

I tried to look arround the Internet and I found the same result as my mine. Is my code wrong somewhere?


回答1:


Retrieve the bitmap image:

public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    if (args.Files.Count > 0)
    {
        var imageFile = args.Files[0] as StorageFile;
        // Ensure the stream is disposed once the image is loaded
        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = new BitmapImage();

            await bitmapImage.SetSourceAsync(fileStream);
            ImageControl.Source = bitmapImage;

            await _viewModel.Upload(imageFile);
        }               
    }
}

Create the file stream:

internal async Task Upload(Windows.Storage.StorageFile file)
{
    var fileStream = await file.OpenAsync(FileAccessMode.Read);
    fileStream.Seek(0);

    var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
    await reader.LoadAsync((uint)fileStream.Size);

    Globals.MemberId = ApplicationData.Current.LocalSettings.Values[Globals.PROFILE_KEY];
    var userName = "Rico";
    var sex = 1;
    var url = string.Format("{0}{1}?memberid={2}&name={3}&sex={4}", Globals.URL_PREFIX, "api/Images", Globals.MemberId, userName,sex);
    byte[] image = new byte[fileStream.Size];

    await UploadImage(image, url);
}

Create a memory stream from the image:

public async Task UploadImage(byte[] image, string url)
{
    Stream stream = new System.IO.MemoryStream(image);
    HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

    Uri resourceAddress = null;
    Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
    Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
    request.Content = streamContent;

    var httpClient = new Windows.Web.Http.HttpClient();
    var cts = new CancellationTokenSource();
    Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
}


来源:https://stackoverflow.com/questions/26223902/window-phone-8-submit-post-form-with-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!