@@ -90,83 +90,48 @@ private static byte[] GetMultipartFormData(Dictionary<string, object> postParame
9090 Stream formDataStream = new MemoryStream ( ) ;
9191 bool needsClrf = false ;
9292
93- if ( postParameters . Count > 1 )
93+ foreach ( var param in postParameters )
9494 {
95- foreach ( var param in postParameters )
95+ // Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
96+ // Skip it on the first parameter, add it to subsequent parameters.
97+ if ( needsClrf )
9698 {
97- // Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
98- // Skip it on the first parameter, add it to subsequent parameters.
99- if ( needsClrf )
100- {
101- formDataStream . Write ( Encoding . UTF8 . GetBytes ( "\r \n " ) , 0 , Encoding . UTF8 . GetByteCount ( "\r \n " ) ) ;
102- }
99+ formDataStream . Write ( Encoding . UTF8 . GetBytes ( "\r \n " ) , 0 , Encoding . UTF8 . GetByteCount ( "\r \n " ) ) ;
100+ }
103101
104- needsClrf = true ;
102+ needsClrf = true ;
105103
106- if ( param . Value is FileInfo )
107- {
108- var fileInfo = ( FileInfo ) param . Value ;
109- string postData =
110- string . Format (
111- "--{0}\r \n Content-Disposition: form-data; name=\" {1}\" ; filename=\" {1}\" \r \n Content-Type: {2}\r \n \r \n " ,
112- boundary ,
113- param . Key ,
114- fileInfo . MimeType ) ;
115- formDataStream . Write ( Encoding . UTF8 . GetBytes ( postData ) , 0 , Encoding . UTF8 . GetByteCount ( postData ) ) ;
116-
117- // Write the file data directly to the Stream, rather than serializing it to a string.
118- formDataStream . Write ( fileInfo . FileContent , 0 , fileInfo . FileContent . Length ) ;
119- }
120- else
121- {
122- var stringDaa = SerializationHelper . Serialize ( param . Value ) ;
123- string postData =
124- string . Format (
125- "--{0}\r \n Content-Disposition: form-data; name=\" {1}\" \r \n \r \n {2}" ,
126- boundary ,
127- param . Key ,
128- stringDaa ) ;
129- formDataStream . Write ( Encoding . UTF8 . GetBytes ( postData ) , 0 , Encoding . UTF8 . GetByteCount ( postData ) ) ;
130- }
104+ if ( param . Value is FileInfo )
105+ {
106+ var fileInfo = ( FileInfo ) param . Value ;
107+ string postData =
108+ string . Format (
109+ "--{0}\r \n Content-Disposition: form-data; name=\" {1}\" ; filename=\" {1}\" \r \n Content-Type: {2}\r \n \r \n " ,
110+ boundary ,
111+ param . Key ,
112+ fileInfo . MimeType ) ;
113+ formDataStream . Write ( Encoding . UTF8 . GetBytes ( postData ) , 0 , Encoding . UTF8 . GetByteCount ( postData ) ) ;
114+
115+ // Write the file data directly to the Stream, rather than serializing it to a string.
116+ formDataStream . Write ( fileInfo . FileContent , 0 , fileInfo . FileContent . Length ) ;
131117 }
132-
133- // Add the end of the request. Start with a newline
134- string footer = "\r \n --" + boundary + "--\r \n " ;
135- formDataStream . Write ( Encoding . UTF8 . GetBytes ( footer ) , 0 , Encoding . UTF8 . GetByteCount ( footer ) ) ;
136- }
137- else
138- {
139- foreach ( var param in postParameters )
118+ else
140119 {
141- if ( param . Value is FileInfo )
142- {
143- var fileInfo = ( FileInfo ) param . Value ;
144-
145- // Write the file data directly to the Stream, rather than serializing it to a string.
146- formDataStream . Write ( fileInfo . FileContent , 0 , fileInfo . FileContent . Length ) ;
147- }
148- else if ( param . Value is byte [ ] )
149- {
150- // Write the file data directly to the Stream, rather than serializing it to a string.
151- formDataStream . Write ( param . Value as byte [ ] , 0 , ( param . Value as byte [ ] ) . Length ) ;
152- }
153- else
154- {
155- string postData ;
156- if ( ! ( param . Value is string ) )
157- {
158- postData = SerializationHelper . Serialize ( param . Value ) ;
159- }
160- else
161- {
162- postData = ( string ) param . Value ;
163- }
164-
165- formDataStream . Write ( Encoding . UTF8 . GetBytes ( postData ) , 0 , Encoding . UTF8 . GetByteCount ( postData ) ) ;
166- }
120+ var stringDaa = SerializationHelper . Serialize ( param . Value ) ;
121+ string postData =
122+ string . Format (
123+ "--{0}\r \n Content-Disposition: form-data; name=\" {1}\" \r \n \r \n {2}" ,
124+ boundary ,
125+ param . Key ,
126+ stringDaa ) ;
127+ formDataStream . Write ( Encoding . UTF8 . GetBytes ( postData ) , 0 , Encoding . UTF8 . GetByteCount ( postData ) ) ;
167128 }
168129 }
169130
131+ // Add the end of the request. Start with a newline
132+ string footer = "\r \n --" + boundary + "--\r \n " ;
133+ formDataStream . Write ( Encoding . UTF8 . GetBytes ( footer ) , 0 , Encoding . UTF8 . GetByteCount ( footer ) ) ;
134+
170135 // Dump the Stream into a byte[]
171136 formDataStream . Position = 0 ;
172137 byte [ ] formData = new byte [ formDataStream . Length ] ;
@@ -227,16 +192,16 @@ private WebRequest PrepareRequest(string path, string method, Dictionary<string,
227192 byte [ ] formData = null ;
228193 if ( formParams . Count > 0 )
229194 {
195+ string formDataBoundary = "GroupDocsConversionForCloud_NETSDKFormBoundary" ;
196+ client . ContentType = "multipart/form-data; boundary=" + formDataBoundary ;
197+
230198 if ( formParams . Count > 1 )
231199 {
232- string formDataBoundary = "GroupDocsConversionForCloud_NETSDKFormBoundary" ;
233- client . ContentType = "multipart/form-data; boundary=" + formDataBoundary ;
234200 formData = GetMultipartFormData ( formParams , formDataBoundary ) ;
235201 }
236202 else
237203 {
238- client . ContentType = "multipart/form-data" ;
239- formData = GetMultipartFormData ( formParams , string . Empty ) ;
204+ formData = GetMultipartFormData ( formParams , formDataBoundary ) ;
240205 }
241206
242207 client . ContentLength = formData . Length ;
0 commit comments