我想问一下如何从Windows Phone 8.1上传图片。 我的解决方案是使用Base64解码,但我认为,这不是最佳方式......
在我的Windows Phone 8.1应用程序中,我选择图像,然后选择我将图像解码为base64。我的想法是我将这个base64字符串作为参数发送到我的WebServise,如此
www.myservice.com/ImageUpload.aspx?img=myBase64String
并将其在服务器上解码回图像文件并上传到服务器。
BUT 这个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();
}
答案 0 :(得分:0)
您可以使用HttpMultipartContent类从Windows手机上传并在服务器端获取。
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。这段代码使用起来非常简单,我觉得比自己转换为字节数组更容易。您需要从服务器端获取此文件。希望这会有所帮助。