如何将jpeg图像转换为字节格式
how to convert jpeg image to byte format
我目前正在使用 Windows phone 应用程序单击图片,我想使用 HTTP post 请求将该图片上传到 Web 服务。我不想使用 windows phone silverlight。
如何将该图像发送到网络服务 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/
我目前正在使用 Windows phone 应用程序单击图片,我想使用 HTTP post 请求将该图片上传到 Web 服务。我不想使用 windows phone silverlight。
如何将该图像发送到网络服务 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/