Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why are you doing the "stringByTrimmingCharactersInSet" call? That would seem to me to remove characters that the encoded image probably needs...</p> <p><em>Edit to include example from some code I'm using which correctly embeds a couple images into a http form to be submitted to a php page.</em></p> <p>I use the following Category on NSMutableData...</p> <p>The following goes inside NSMutableData+HTTP.h</p> <pre><code>@interface NSMutableData (HTTP) -(void)appendFileAtPath:(NSString*)fullFilePath withFormKey:(NSString*)formKey withSuggestedFileName:(NSString*)suggestedFileName boundary:(NSData*)boundary; -(void)appendKey:(NSString*)key value:(NSString*)value boundary:(NSData*)boundary; @end </code></pre> <p>The following goes inside NSMutableData+HTTP.m</p> <pre><code>#import "NSMutableData+HTTP.h" @implementation NSMutableData (HTTP) //use for attaching a file to the http data to be posted -(void)appendFileAtPath:(NSString*)fullFilePath withFormKey:(NSString*)formKey withSuggestedFileName:(NSString*)suggestedFileName boundary:(NSData*)boundary{ [self appendData:boundary]; [self appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", formKey, suggestedFileName] dataUsingEncoding:NSUTF8StringEncoding]]; [self appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [self appendData:[NSData dataWithContentsOfFile:fullFilePath]]; [self appendData:[@"Content-Encoding: gzip\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [self appendData:boundary]; } //use for attaching a key value pair to the http data to be posted -(void)appendKey:(NSString*)key value:(NSString*)value boundary:(NSData*)boundary{ [self appendData:boundary]; [self appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [self appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; [self appendData:boundary]; } @end </code></pre> <p>Then in my code that creates an http form submission I do the following to attach a couple images and key-value pairs (via the category methods above) to the form... (Note: I have not included the NSURLConnectionDelegate methods that you'll need to implement if you want to track the progress of the form submission.)</p> <pre><code>NSString *boundaryString = @"0xKhTmLbOuNdArY"; NSData *boundaryData = [[NSString stringWithFormat:@"\r\n--%@\r\n", boundaryString] dataUsingEncoding:NSUTF8StringEncoding]; NSString *phpReceiverURL = @"http://www.someurl.com/somephppage.php"; NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init]; [theRequest setTimeoutInterval:30]; [theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [theRequest setHTTPShouldHandleCookies:NO]; [theRequest setURL:[NSURL URLWithString:phpReceiverURL]]; [theRequest setHTTPMethod:@"POST"]; [theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundaryString] forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; //add some image files to the form [body appendFileAtPath:[imagePath stringByAppendingPathComponent:@"cat.jpg"] withFormKey:@"cat" withSuggestedFileName:@"received-cat.jpg" boundary:boundaryData ]; [body appendFileAtPath:[imagePath stringByAppendingPathComponent:@"dog.jpg"] withFormKey:@"dog" withSuggestedFileName:@"received-dog.jpg" boundary:boundaryData ]; //add some key value pairs to the form [body appendKey:@"testkeyone" value:@"testvalueone" boundary:boundaryData]; [body appendKey:@"testkeytwo" value:@"testvaluetwo" boundary:boundaryData]; // setting the body of the post to the reqeust [theRequest setHTTPBody:body]; //create the connection NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; </code></pre> <p>Also note I haven't tested this outside of my app so I may have forgotten something (such as "imagePath" which I haven't defined in the example above - you'll have to specify a path for your images), but it should give you a good idea of how to attach some images to a form to be submitted to a php page. </p> <p>Hope this helps!</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.
 

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