首页 > Web开发 > 详细

Xamarin.Forms 使用HttpClient上传文件

时间:2018-02-13 20:53:45      阅读:763      评论:0      收藏:0      [点我收藏+]

Xamarin.Forms 使用HttpClient上传文件

在应用开发中,上传图片很多时候都是不可避免的问题;

以下用HttpClient实现的上传文件代码:

 

 

 1  public static async Task<string> UploadFileAsync(string url ,string path)
 2         {
 3             using (var client = new HttpClient())
 4             {
 5                 using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.Ticks.ToString("x")))
 6                 {
 7                     var upfilebytes = File.ReadAllBytes(path);
 8                     var ms = new MemoryStream(upfilebytes);
 9                     content.Add(new StreamContent(ms), "file", "upload.jpg");
10                     using (var httpResponseMessage = await client.PostAsync(url, content))
11                     {
12                         var responseContent = "";
13                         if (httpResponseMessage.IsSuccessStatusCode)
14                         {
15                             responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
16                         }
17                         return responseContent;
18                     }
19                 }
20             }
21         }

 

Xamarin.Forms 使用HttpClient上传文件

原文:https://www.cnblogs.com/devin_zhou/p/8442650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!