0
我目前使用Windows手机应用程序来点击图片,并且我想使用HTTP发布请求将该图片上传到网络服务。我不想使用Windows Phone Silverlight。如何将jpeg图像转换为字节格式
如何将该图像发送到Web服务URL?
我目前使用Windows手机应用程序来点击图片,并且我想使用HTTP发布请求将该图片上传到网络服务。我不想使用Windows Phone Silverlight。如何将jpeg图像转换为字节格式
如何将该图像发送到Web服务URL?
在http上发布图像就像发布任何其他文件类型一样。使用下面的代码片段
public string PostFileUsingApi()
{
string result = "";
string param1 = "value1";
using (var handler = new HttpClientHandler()) {
using (var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost:8008") }) {
client.Timeout = new TimeSpan(0, 20, 0);
StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
Stream stream = await storageFile.OpenStreamForReadAsync();
var requestContent = new MultipartFormDataContent();
StreamContent fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "imagekey", //content key goes here
FileName = "myimage"
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/bmp");
requestContent.Add(fileContent);
client.DefaultRequestHeaders.Add("ClientSecretKey", "ClientSecretValue");
HttpResponseMessage response = await client.PostAsync("api/controller/UploadData?param1=" + HttpUtility.UrlEncode(param1), requestContent).Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK) {
result = await response.Content.ReadAsStringAsync().Result
} else {
result = "";
}
}
}
return result;
}
安装这个包来解决依赖性 https://www.nuget.org/packages/microsoft.aspnet.webapi.client/
但没有FILESTREAM为以system.IO – chinna2580
我已经编辑代码尖晶石工作,甚至我已经添加的Windows Phone 8应用程序集引用与Windows手机流。在沙箱环境中,您无法使用IO Stream访问文件,您需要从Windows Phone中的StorageFile对象获取流。 – Zain
@Zain与'Async'一起使用'await'方法 – Eldho