这是webserice电话
URL: http://mywedshare.com/wp-admin/admin-ajax.php?action=app_photoupload
gallery_ID,guestName, guestEmail, guestPasscode,photo_file (Multipart File Format)
我需要上传UIImage。我使用以下行获取我的NSData -
imageData = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 1);
并使用
上传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");
}
}];
我获得了成功但图像本身没有上传到服务器上。请帮我。
答案 0 :(得分:0)
我建议:
在模拟器上运行应用程序并同时运行Charles(或类似的东西),并确保请求看起来不错。我从客户端看不到任何明显的错误。不过,查尔斯可以告诉我们请求是否正常。
我显然无法对服务器端说话(例如选择photo_file
字段名称,其他属性等)。您对API有多大信心?
如果statusCode
为200
,您宣布成功。 200
是HTTP级别状态代码,表示收到请求并且服务器成功响应,但这并不一定意味着上载成功。看起来该URL返回了一些代码,您可能希望在开始声明成功之前检查该值。