[英]Upload image from WIndows phone 8.1 to web
我想问一下如何从Windows Phone 8.1上传图像。 我的解决方案是使用Base64解码,但是我认为这不是最好的方法。
在Windows Phone 8.1应用程序中,我选择图像,然后选择将图像解码为base64。 我的想法是将这个base64字符串作为这样的参数发送到WebServise
www.myservice.com/ImageUpload.aspx?img=myBase64String
并将其在服务器上解码回映像文件并上传到服务器。
但是此base64字符串太长,因此Web服务返回错误“请求URL太长”。
从Windows Phone 8.1将图像文件上传到Web的另一种方法是
哪个更好。 将图像作为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();
}
您可以使用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。 这段代码使用起来非常简单,我发现它比自己转换为字节数组更容易。 您需要从服务器端获取此文件。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.