Ios 使用多部分/表单数据目标C上载图像
我是新的iOS开发者。我想上传带有多部分/表单数据的图像,这是我的obj C代码,我是从邮递员那里得到的。图为API的请求参数。我在邮递员测试它工作正常,但我在移动测试它不工作,并得到HTTP 500错误,请为我检查它。谢谢大家Ios 使用多部分/表单数据目标C上载图像,ios,objective-c,iphone,Ios,Objective C,Iphone,我是新的iOS开发者。我想上传带有多部分/表单数据的图像,这是我的obj C代码,我是从邮递员那里得到的。图为API的请求参数。我在邮递员测试它工作正常,但我在移动测试它不工作,并得到HTTP 500错误,请为我检查它。谢谢大家 您可以使用第三方类。 参考: NSMutableURLRequest*请求=[[AFHTTPRequestSerializer]multipartFormRequestWithMethod:@“POST”URLString:@”http://example.com/up
您可以使用第三方类。 参考:
NSMutableURLRequest*请求=[[AFHTTPRequestSerializer]multipartFormRequestWithMethod:@“POST”URLString:@”http://example.com/upload“参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@]file://path/to/image.jpg“]name:@“file”fileName:@“fileName.jpg”mimeType:@“image/jpeg”错误:nil];
}错误:无];
AFURLSessionManager*manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask*上传任务;
uploadTask=[manager]
uploadTaskWithStreamedRequest:请求
进度:^(NSProgress*_非空上传进度){
//这不会在主队列上回调。
//您负责将UI更新发送到主队列
dispatch\u async(dispatch\u get\u main\u queue()^{
//更新进度视图
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse*\u非空响应,id\u可空响应对象,NSError*\u可空错误){
如果(错误){
NSLog(@“错误:%@”,错误);
}否则{
NSLog(@“%@%@”,响应,响应对象);
}
}];
[上传任务恢复];
500是内部服务器错误,您应该检查服务器日志。我不是在使用服务器,我只是收到API并使用它。谢谢,我使用了AFNetworking类,但它仍然不工作。这是我的代码:(。
strURL = [NSString stringWithFormat:@"%@%@",URL,strURL];
NSArray *parameters = @[ @{ @"name": @"imageInfo", @"value": @"{\"user\":{\"userId\":\"165\"},\"description\":\"test description\",\"competitionId\":\"1\",\"speciesId\":\"13\",\"lng\":\"107.9296875\",\"lat\":\"15.83453574122155\",\"locationName\":\"Đại Lãnh, Đại Lộc, Quảng Nam, Vietnam\",\"originalImageName\":\"\"}" },
@{ @"name": @"filenames", @"value": @"{}" },
@{ @"name": @"files", @"fileName": (UIImageJPEGRepresentation(uploadImage, 0.1)) } ];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
NSString *boundary = @"---011000010111000001101001";
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",
@"cache-control": @"no-cache",
@"postman-token": @"463bcf06-08f6-7611-c1fb-13b0dde79c5c"};
NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
[body appendFormat:@"\r\n--%@\r\n", boundary];
if (param[@"fileName"]) {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], @"yourimage.jpg"];
[body appendFormat:@"Content-Type: image/jpeg\r\n\r\n"];
[body appendFormat:@"%@", param[@"fileName"]];
if (error) {
NSLog(@"%@", error);
}
} else {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
[body appendFormat:@"%@", param[@"value"]];
}
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLResponse *response;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];