简体   繁体   中英

Upload image file ios - Multipart format

Here is the webserice call

URL: http://mywedshare.com/wp-admin/admin-ajax.php?action=app_photoupload
gallery_ID,guestName, guestEmail, guestPasscode,photo_file (Multipart File Format) 

I need to upload an UIImage. Im getting my NSData using the following line -

   imageData = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 1);

and uploading it using

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

//Populate a dictionary with all the regular values you would like to send.?action=app_photoupload?
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:@"app_photoupload" forKeyPath:@"action"];
[parameters setValue:galleryID forKey:@"gallery_ID"];

[parameters setValue:galleryName forKey:@"guestName"];

[parameters setValue:guestEmail forKey:@"guestEmail"];
[parameters setValue:guestPasscode forKey:@"guestPasscode"];


// add params (all params are strings)
for (NSString *param in parameters) {
    [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", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

NSString *FileParamConstant = @"photo_file";

NSData *imageData2 = imageData;

//Assuming data is not nil we add this to the multipart form
if (imageData2)
{
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"wedshare.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData2];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the request
[request setHTTPBody:body];

// set URL
NSString *url = @"http://mywedshare.com/wp-admin/admin-ajax.php";

[request setURL:[NSURL URLWithString:url]];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

                           if ([httpResponse statusCode] == 200) {
                               [HUD showUIBlockingIndicatorWithText:@"Success"];
                               [HUD hideUIBlockingIndicator];


                               NSLog(@"success");
                           }

                       }];

Im getting Success but the image itself is not being uploaded onto the server. Please help me.

I'd suggest:

  • Run the app on the simulator and run Charles (or something like it) at the same time, and make sure the request looks ok. I don't see anything obviously wrong here from the client side. Still, Charles can tell us if the request looks ok.

  • I obviously cannot speak to the server side of it (eg the choice of the photo_file field name, the other properties, etc.). How confident are you regarding the API?

  • You are declaring success if the statusCode is 200 . The 200 is a HTTP level status code that means is that the request was received and the server responded successfully, but it doesn't necessarily mean that the upload was successful. It looks like that URL returns some code, and you might want to examine that value before you start declaring success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM