httpclient上傳圖片(multipart/form-data)

string boundary = string.Format("----WebKitFormBoundary{0}", DateTime.Now.Ticks.ToString("x"));
                MultipartFormDataContent content = new MultipartFormDataContent(boundary);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
                string fileName = Path.GetFileName(picPath);

                using FileStream fStream = File.Open(picPath, FileMode.Open, FileAccess.Read);
                content.Add(new StreamContent(fStream, (int)fStream.Length), "file", fileName);

                using HttpClient client = new HttpClient();
                HttpResponseMessage response = client.PostAsync(url, content).Result;

                if (response.IsSuccessStatusCode)
....

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章