Window Phone 8用图像提交信息 [英] Window Phone 8 submit post form with an image

查看:90
本文介绍了Window Phone 8用图像提交信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用window phone 8.0,并且在尝试从移动电话向网站提交帖子请求时遇到问题。

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

我使用PhotoPicker从libary中选择照片并保存到照片流式照片= e.ChosenPhoto; 并且完美无缺。

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?

推荐答案

检索位图图像:

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);
        }               
    }
}

创建文件stream:

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);
}

这篇关于Window Phone 8用图像提交信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆