【问题标题】:Upload image from WIndows phone 8.1 to web将图像从 Windows phone 8.1 上传到网络
【发布时间】:2015-11-30 18:14:01
【问题描述】:

我想问一下如何从Windows phone 8.1 上传图片。 我的解决方案是使用 Base64 解码,但我认为这不是最好的方法......

在我的 Windows Phone 8.1 应用程序中,我选择图像并在选择后将图像解码为 base64。我的想法是我将这个 base64 字符串作为参数发送到我的 WebServise

 www.myservice.com/ImageUpload.aspx?img=myBase64String

并在服务器上将其解码回图像文件并上传到服务器。

但是 这个 base64 字符串太长,所以 webservice 返回错误“请求 URL 太长。”

另外一种将图像文件从 Windows Phone 8.1 上传到网络的方法

哪个更好。将图像作为 base64 字符串或 BLOB 保存到数据库。 谢谢

编辑:

Windows phone 中的请求:

public async Task<string> ImageToBase64(StorageFile MyImageFile)
        {
            Stream ms = await MyImageFile.OpenStreamForReadAsync();
            byte[] imageBytes = new byte[(int)ms.Length];
            ms.Read(imageBytes, 0, (int)ms.Length);

            string base64Image = Convert.ToBase64String(imageBytes);
            string url = "http://mobil.aspone.com/ImageUpload.aspx?Id=" + base64Image;
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(new Uri(url));
            var result = await response.Content.ReadAsStringAsync();
            string jsonString = result.ToString();
            return jsonString;
    }

ASP.NET 服务

 protected void Page_Load(object sender, EventArgs e)
        {
            string Id = Request.QueryString["Id"];
            //byte[] bytes = Convert.FromBase64String(Id);

            Image1.ImageUrl = "data:image/png;base64," + Id;

If(Id!=null)
{
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
             Response.Write("Uploaded");
            Response.End();
    }
else
{
Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
             Response.Write("Failed");
            Response.End();
    }

【问题讨论】:

  • 如果 URL 太长,为什么不把它放在请求的正文中?
  • 在体内?我应该怎么做?您能解释一下或发布示例吗?我从不对身体提出要求
  • 一开始你还没有展示你是如何处理请求的。更新您的代码以显示这一点,然后向您展示如何将数据放入正文中会更容易。
  • 帖子已更新
  • 所以您使用的是 HttpClient 库。它有一个PostAsync 方法,允许您添加一个对象作为请求正文的一部分。查看this tutorialthis question,它们应该让您了解如何使用它。顺便说一句,图像不是基于 64 位的字符串,因此将它们作为字节数组传输和存储对我来说更有意义。这将减少不必要的转换量。

标签: c# asp.net .net windows-phone-8.1 windows-phone


【解决方案1】:

您可以使用 HttpMultipartContent 类从 windows phone 上传并在服务器端获取它。

HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
                    var stream = await PickedFile.OpenAsync(FileAccessMode.Read);
                    form.Add(new HttpStreamContent(stream), "image");
                    var Response = await new HttpClient().PostAsync(new Uri(StringUtility.UploadImageUrl), form);

PickedFile 在这里是 StorageFile。这段代码使用起来非常简单,我发现它比自己转换为字节数组更容易。您需要从服务器端获取此文件。希望这会有所帮助。

【讨论】:

  • 谢谢,但我遇到了这个错误:{System.ArgumentException:值不在预期范围内。在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task).... 应用程序在此行崩溃: var Response = await new HttpClient().PostAsync(new Uri(StringUtility.UploadImageUrl), form);我不知道为什么......
  • 在设置传递数据的内容长度时可能会发生这种情况。 [monkeyweekend.wordpress.com/2014/10/23/…按照以下步骤使用Request.Files上传和获取文件
  • 如果上一个链接不适合你,这里又是:monkeyweekend.com/2014-10-23/…
  • 如果您的文件为空(零长度),PostAsync 上的 System.ArgumentException 可能是。
  • 是 Silverlight 还是 Runtime?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
相关资源
最近更新 更多