Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to render my comment more precisely:</p> <p>Your code looks <em>almost</em> correct, but there are a few issues:</p> <p>First, a "boundary-delimiter" MUST be preceded by a CRLF. And, conceptually, this preceding CRLF belongs to the "boundary-delimiter.</p> <p>Second, a "Content-Type" header for parameters won't hurt.</p> <p>Thus, I would suggest to change the following from</p> <pre><code>// add params (all params are strings) for (NSString *param in _params) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } </code></pre> <p>to </p> <pre><code>// add params (all params are strings) for (NSString *param in _params) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendBytes:"Content-Type: text/plain; charset=utf-8\r\n" length:41]; // (other headers if any) // close headers with CRLF: [body appendBytes:"\r\n" length:2]; // param value: [body appendData:[[NSString stringWithFormat:@"%@", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } </code></pre> <p>Forth, you should not append a CRLF to binary data! When outputting a part which contains binary data, follow the above lines: as you can see, there is no closing CRLF after writing the parameter value to the body part.</p> <p>Hence:</p> <pre><code>if (imageData) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"harryimg1.png\"\r\n",FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; } </code></pre> <p>Furthermore, you omitted the "close-boundary-delimiter:</p> <p>After the last part, add the following:</p> <pre><code>[body appendData:[[NSString stringWithFormat:@"\r\n--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; </code></pre> <p>A last CRLF is not required, but won't hurt also.</p> <h3>Edit</h3> <p>Further notes:</p> <p>It's not recommended to use <code>NSURLConnection</code>'s convenient class method sendSynchronousRequest: when sending large data. You are better of implementing NSURLConnection delegates which lets you cancel the request, when something goes wrong, and also gives more control.</p> <p>Furthermore, this approach uses a <code>NSData</code> as the request body of the multipart/form-data message. This requires that the while body must fit into memory. This can lead to memory issues. A more robust approach would utilize a input stream as request body, but this implementation is much more complex (really MUCH more).</p> <p>iOS 7 has a new API where it should be more easy to transmit large files. Please consult the documentation of <code>NSURLSession</code> and <code>NSURLSessionUploadTask</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload