上传文件

using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient())
{
Windows.Web.Http.HttpMultipartFormDataContent form = new Windows.Web.Http.HttpMultipartFormDataContent();

Windows.Web.Http.HttpStreamContent snContent = new Windows.Web.Http.HttpStreamContent(((Stream)new MemoryStream(Encoding.UTF8.GetBytes(sn))).AsInputStream());
snContent.Headers.ContentDisposition = new Windows.Web.Http.Headers.HttpContentDispositionHeaderValue("form-data");
form.Add(snContent, "sn");

Windows.Web.Http.HttpStreamContent fileContent = new Windows.Web.Http.HttpStreamContent(await file.OpenReadAsync());
fileContent.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/octet-stream");//image/png"
//  这样设置无效
//fileContent.Headers.ContentDisposition = new Windows.Web.Http.Headers.HttpContentDispositionHeaderValue("form-data");
//fileContent.Headers.ContentDisposition.Name = "Filedata";
#endregion
byte[] buffer = Encoding.UTF8.GetBytes(file.Name);
form.Add(fileContent, "Filedata", Encoding.UTF8.GetString(buffer, 0, buffer.Length));

client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(new Uri(TotalInterfaceDocument.updateUserPicInterface), form).AsTask();
}

  

原文地址:https://www.cnblogs.com/lj940306/p/4677882.html