我正在使用POST方法和multipartMethod将图像从iphone上传到服务器。我的Objective-C代码是
maxDocuments
我的PHP文件(update_profile.php)代码是
numBytes
我在其他项目中使用了相同的代码,但是在这个项目中工作得很好但是没有工作。问题与EL Capitan OSX有关吗?因为我更新了我的操作系统。任何人都知道我哪里错了?任何帮助,将不胜感激。感谢。
错误是: 服务器端未收到数据和图像。没有任何内容($ _ FILES),我也检查了$ _POST和$ _REQUEST,但是服务器没有收到“a:0 {}”保存在'newfile.txt'中的任何内容。我在服务器上创建文件'newfile.txt',但此文件中没有任何内容。
答案 0 :(得分:1)
//对于Objective-c代码 - 将uiimage转换为nsdata // ---- //注意:在php端编写转换json字符串到图像的编码,轻松找到谷歌 // ---
NSData *data1 = UIImageJPEGRepresentation(imgProfileView.image,0.8); //0.1 to 1.0 upto image resolution
NSString *encodedString1 = [data1 base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSMutableDictionary *dicImage=[[NSMutableDictionary alloc] init];
[dicImage setObject:encodedString1 forKey:@"image1"];
[dicImage setObject:[dic valueForKey:@"des1"] forKey:@"des1"]; // if added any extra filed then
NSMutableDictionary *dic_set_Sync=[[NSMutableDictionary alloc] init];
[dic_set_Sync setObject:APPDELEGATE.strEmailId forKey:@"email"];
[dic_set_Sync setObject: dicImage forKey:@"compressor_sync"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dic_set_Sync options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
NSURL *url = [NSURL URLWithString:@"https://www.inspectab.com/app/webservice/offline/compressor_syc.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *params = [NSString stringWithFormat:@"json=%@",
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:paramsData];
NSURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);
// examine the response
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"responseString: %@",responseString);
if(![responseString isEqualToString:@""])
{
NSDictionary *dicResponse = [NSJSONSerialization JSONObjectWithData:returnData
options:kNilOptions error:&error];
NSLog(@"dicResponse: %@", dicResponse);
}
答案 1 :(得分:0)
我已经使用以下代码进行图片上传,我认为这可能会有所帮助 -
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@“youUrl” parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
} error:nil];
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
// [self.progressView setProgress:uploadProgress.fractionCompleted];
NSLog(@"Proggress%f",uploadProgress.fractionCompleted);
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
[HUD setHidden:YES];
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Please check Internet Connection!" message:@"Error" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
} else {
// [HUD show:NO];
[HUD setHidden:yes];
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];