Upload multiple files using http post

I have a problem downloading FTP files from my iPhone application.

I am sending an HTTP POST request to my web service with all the necessary parameters. I am sending two files, one is an image and the other is audio. The image is loading, but there is no sound. When I spend my web service, it only shows the image field as a downloaded file. He does not show that there is sound either.

My code is:

 NSString *urlString = [NSString stringWithFormat:ADD_LEAD_API_URL]; NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; // change type to POST (default is GET) [postRequest setHTTPMethod:@"POST"]; // just some random text that will never occur in the body NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; // header value NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; // set header [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; // create data NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // message part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"firstName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; /*** My rest of string parameters are successfully added to request ***/ // media part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageUrl\"; filename=\"dummy.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // get the image data from main bundle directly into NSData object UIImage *img= leadImage; NSData *imageData= UIImageJPEGRepresentation(img,90); [postBody appendData:imageData]; // Image is being uploaded [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"recordSoundUrl\"; filename=\"%@\"\r\n", self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]]; [postBody appendData:soundData]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // final boundary [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; // add body to post [postRequest setHTTPBody:postBody]; // Asynch request NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self]; 

If I do not upload an image, then it takes sound. Therefore, I can send only one file with the request. Please help me and tell me if I'm wrong somewhere.

Thanks in advance.

+6
source share
3 answers

You used ASIHTTPRequest ASINetworkQueue to send multiple files.

UPDATE: As indicated below, ASIHTTPRequest is no longer supported. Use caution with this structure. Other options are MKNetworkKit or AFNetworking .

+2
source

It is pretty simple. Just form a multi-part body. Suppose you have files to load and regular parameters in the params dictionary:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:54.0]; NSMutableData* body = [NSMutableData data]; NSString* boundary = [NSString randomStringWithLength:64]; [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; NSData *boundaryData = [[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]; [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [body appendData:boundaryData]; // I use special simple class to distinguish file params if( [obj isKindOfClass:[FileUpload class]] ) { // File upload [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", key, [obj localName]] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData: [obj loadData]]; // It just return NSData with loaded file in it [body appendData: [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; } else { // Regular param [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n", key, obj] dataUsingEncoding:NSUTF8StringEncoding]] } }]; [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:body]; 

Now you can simply send it in any way:

 [NSURLConnection sendAsynchronousRequest:request queue:queueOfYourChoice completionHandler:^(NSURLResponse *response, NSData *rdata, NSError *error) 

The FileUpload class is pretty trivial: it is created from url / filename, provides that name as a method, and loads binary content into NSData *. I do not list it for clarity, but add it if necessary.

+7
source

You may have a problem setting the border.

Use it as a border

 NSString boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 

and use this

 [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

instead of this

 [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
0
source

Source: https://habr.com/ru/post/886237/


All Articles