11using System ;
22using System . Collections . Generic ;
3+ using System . Collections . Specialized ;
34using System . IO ;
45using System . Linq ;
56using System . Net ;
@@ -31,6 +32,7 @@ private void WriteToStream(MemoryStream stream, StringBuilder requestContent, st
3132 private byte [ ] GetMultipartFormData ( Dictionary < string , object > postParameters , string boundary , StringBuilder requestContent )
3233 {
3334 var needsCLRF = false ;
35+ var sb = new StringBuilder ( ) ;
3436
3537 using ( var stream = new MemoryStream ( ) )
3638 {
@@ -45,18 +47,23 @@ private byte[] GetMultipartFormData(Dictionary<string, object> postParameters, s
4547
4648 if ( param . Value is ApiFileParameter )
4749 {
48- var fileToUpload = ( ApiFileParameter ) param . Value ;
50+ var file = ( ApiFileParameter ) param . Value ;
4951
50- string header = string . Format ( "--{0}\r \n Content-Disposition: form-data; name=\" {1}\" ; filename=\" {2}\" \r \n Content-Type: {3}\r \n \r \n " ,
51- boundary ,
52- param . Key ,
53- fileToUpload . FileName ?? param . Key ,
54- fileToUpload . ContentType ?? "application/octet-stream" ) ;
52+ sb . Clear ( ) ;
53+ sb . AppendFormat ( "--{0}\r \n " , boundary ) ;
54+ sb . AppendFormat ( "Content-Disposition: form-data; name=\" {0}\" ; filename=\" {1}\" " , param . Key , file . FileName ?? param . Key ) ;
55+
56+ foreach ( var key in file . Parameters . AllKeys )
57+ {
58+ sb . AppendFormat ( "; {0}=\" {1}\" " , key , file . Parameters [ key ] . Replace ( '"' , '\' ' ) ) ;
59+ }
60+
61+ sb . AppendFormat ( "\r \n Content-Type: {0}\r \n \r \n " , file . ContentType ?? "application/octet-stream" ) ;
5562
56- WriteToStream ( stream , requestContent , header ) ;
63+ WriteToStream ( stream , requestContent , sb . ToString ( ) ) ;
5764
58- stream . Write ( fileToUpload . File , 0 , fileToUpload . File . Length ) ;
59- requestContent . AppendFormat ( "<Binary image data here (length {0} bytes)...>" , fileToUpload . File . Length ) ;
65+ stream . Write ( file . Data , 0 , file . Data . Length ) ;
66+ requestContent . AppendFormat ( "<Binary image data here (length {0} bytes)...>" , file . Data . Length ) ;
6067 }
6168 else
6269 {
@@ -114,6 +121,7 @@ public Dictionary<string, object> CreateProductImageMultipartData(List<string> f
114121 if ( ! string . IsNullOrEmpty ( sku ) )
115122 dic . Add ( "Sku" , sku ) ;
116123
124+ // also possible:
117125 //if (!string.IsNullOrEmpty(gtin))
118126 // dic.Add("Gtin", sku);
119127
@@ -125,7 +133,14 @@ public Dictionary<string, object> CreateProductImageMultipartData(List<string> f
125133 fstream . Read ( data , 0 , data . Length ) ;
126134
127135 var name = Path . GetFileName ( path ) ;
128- dic . Add ( string . Format ( "my-image{0}" , ++ count ) , new ApiFileParameter ( data , name , MimeMapping . GetMimeMapping ( name ) ) ) ;
136+ var id = string . Format ( "my-image{0}" , ++ count ) ;
137+ var apiFile = new ApiFileParameter ( data , name , MimeMapping . GetMimeMapping ( name ) ) ;
138+
139+ // test pass through of custom parameters
140+ apiFile . Parameters . Add ( "CustomValue1" , string . Format ( "{0:N}" , Guid . NewGuid ( ) ) ) ;
141+ apiFile . Parameters . Add ( "CustomValue2" , string . Format ( "say hello to {0}" , id ) ) ;
142+
143+ dic . Add ( id , apiFile ) ;
129144
130145 fstream . Close ( ) ;
131146 }
@@ -258,23 +273,26 @@ public bool ProcessResponse(HttpWebRequest webRequest, WebApiConsumerResponse re
258273
259274 public class ApiFileParameter
260275 {
261- public ApiFileParameter ( byte [ ] file )
262- : this ( file , null )
276+ public ApiFileParameter ( byte [ ] data )
277+ : this ( data , null )
263278 {
264279 }
265- public ApiFileParameter ( byte [ ] file , string filename )
266- : this ( file , filename , null )
280+ public ApiFileParameter ( byte [ ] data , string filename )
281+ : this ( data , filename , null )
267282 {
268283 }
269- public ApiFileParameter ( byte [ ] file , string filename , string contenttype )
284+ public ApiFileParameter ( byte [ ] data , string filename , string contenttype )
270285 {
271- File = file ;
286+ Data = data ;
272287 FileName = filename ;
273288 ContentType = contenttype ;
289+ Parameters = new NameValueCollection ( ) ;
274290 }
275291
276- public byte [ ] File { get ; set ; }
292+ public byte [ ] Data { get ; set ; }
277293 public string FileName { get ; set ; }
278294 public string ContentType { get ; set ; }
295+
296+ public NameValueCollection Parameters { get ; set ; }
279297 }
280298}
0 commit comments