我试图将图片发布为byte
数组。
我已经使用以下代码将图像转换为字节,现在我想将其发布到服务器。
WriteableBitmap btmMap = new WriteableBitmap(bi);
System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, 200, 200, 0, 100);
result = ms.ToArray();
我现在如何执行POST
操作?
答案 0 :(得分:0)
我建议将MIME multipart作为内容类型。如果你问我,它是最适合字节数组的内容类型。实现这一目标的一种方法:
$.getJSON("../JSON/list_data.json", function(info){
$(document).on("pageinit", "#mainList", function () {
var li = "";
$.each(data, function (i, name) {
li += '<li><a href="#" id="' + i + '" class="info-go">' + name.name + '</a></li>';
$("#mainList").append(li).promise().done(function () {
$(this).on("click", ".info-go", function (e) {
e.preventDefault();
$("#secondaryDetails").data("info", info[this.id]);
index.html#secondaryDetails
$.mobile.changePage("#secondaryDetails");
});
$(this).listview("refresh");
});
$(document).on("pagebeforeshow", "#secondaryDetails", function () {
var info = $(this).data("info");
var info_view = "";
for (var key in info) {
info_view += '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar field" style="font-weight : bold; text-align: left;">' + key + '</div></div><div class="ui-block-b"><div class="ui-bar value" style="width : 75%">' + info[key] + '</div></div></div>';
}
$(this).find("[data-role=content]").html(info_view);
});
});
});
});
});
答案 1 :(得分:0)
尝试以下代码:
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
var imageForm = new ByteArrayContent(result, 0, result.Count());
imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
form.Add(imagenForm, "image", "nameholder.jpg");
HttpResponseMessage response = await httpClient.PostAsync("URL_here", form);
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
httpClient.Dispose();
希望这有助于您了解如何将byte arry to POST
传递给API调用。
修改强>
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StringContent(UserID), "UserID");
var imageForm = new ByteArrayContent(result, 0, result.Count());
imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
form.Add(imagenForm, "image", "nameholder.jpg");
HttpResponseMessage response = await httpClient.PostAsync("URL_here", form);
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
httpClient.Dispose();