将图像从WIndows phone 8.1上传到web

本文关键字:web 图像 WIndows phone | 更新日期: 2023-09-27 18:25:02

我想问一下如何从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手机中的请求:

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

将图像从WIndows phone 8.1上传到web

您可以使用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。这段代码使用起来非常简单,我发现它比自己转换为字节数组更容易。您需要从服务器端获取此文件。希望这能有所帮助。