2014-10-12 25 views
0

我想發佈的multipart/form-data的使用HttpClient的多文件與HttpClient的發送的multipart/form-data的

形式需要特定數量的圖像。

代碼:

var client = new System.Net.Http.HttpClient(); 
var content = new MultipartFormDataContent(); 
var postData = new List<KeyValuePair(string,string)>(); 
postData.Add(new KeyValuePair < string, string > ("function", "picture2")); 
postData.Add(new KeyValuePair < string, string > ("username ", UserID)); 
postData.Add(new KeyValuePair < string, string > ("password ", Password)); 
foreach(var keyValuePair in postData) { 
content.Add(new StringContent(keyValuePair.Value), 
    String.Format("\"{0}\"", keyValuePair.Key)); 
} 
int x = 1; 
foreach(Bitmap item in newpics) { 
using(MemoryStream ms = new MemoryStream()) { 
    item.Save(ms, ImageFormat.Bmp); 
    byte[] bits = ms.ToArray(); 
    content.Add(new ByteArrayContent(bits), '"' + "pict" + x + '"'); 
    x += 1; 
} 
} 

的問題是,只有最後一個圖像傳送!

爲什麼會發生這種情況?我錯過了什麼?以及如何解決這個問題?

在此先感謝..

回答

0

這是如何使用MultipartFormDataContent發佈的HTTPClient字符串和文件流的例子。 Content-Disposition和Content-Type需要爲每個HTTPContent指定:

這是我的例子。希望它有幫助:

var path = @「C:\ B2BAssetRoot \ files \ 596086 \ 596086.1.mp4」;

 using (var client = new HttpClient()) 
     { 
      client.DefaultRequestHeaders.Add("User-Agent", "CBS Brightcove API Service"); 

      using (var content = new MultipartFormDataContent()) 
      { 
       string assetName = Path.GetFileName(path); 

       var request = new HTTPBrightCoveRequest() 
        { 
         Method = "create_video", 
         Parameters = new Params() 
          { 
           CreateMultipleRenditions = "true", 
           EncodeTo = EncodeTo.Mp4.ToString().ToUpper(), 
           Token = "x8sLalfXacgn-4CzhTBm7uaCxVAPjvKqTf1oXpwLVYYoCkejZUsYtg..", 
           Video = new Video() 
            { 
             Name = assetName, 
             ReferenceId = Guid.NewGuid().ToString(), 
             ShortDescription = assetName 
            } 
          } 
        }; 

       //Content-Disposition: form-data; name="json" 
       var stringContent = new StringContent(JsonConvert.SerializeObject(request)); 
       stringContent.Headers.Add("Content-Disposition", "form-data; name=\"json\""); 
       content.Add(stringContent, "json"); 


       FileStream fs = File.OpenRead(path); 

       var streamContent = new StreamContent(fs); 
       streamContent.Headers.Add("Content-Type", "application/octet-stream"); 
       //Content-Disposition: form-data; name="file"; filename="C:\B2BAssetRoot\files\596090\596090.1.mp4"; 
       streamContent.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + Path.GetFileName(path) + "\""); 
       content.Add(streamContent, "file", Path.GetFileName(path)); 

       //content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 



       Task<HttpResponseMessage> message = client.PostAsync("http://api.brightcove.com/services/post", content); 

       var input = message.Result.Content.ReadAsStringAsync(); 
       Console.WriteLine(input.Result); 
       Console.Read(); 
相關問題