Xamarin.Forms 使用HttpClient上傳文件


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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM