上传图片使用AFnetwork 3多部分获取零参数
问题描述:
目前,我正在开发一个项目。我正在使用网络上传带有参数的图像。它在wifi上工作正常,但是如果我使用蜂窝网络。它没有正确传递参数。就像后台服务器接收参数的次数一样。有时会丢失。我使用的代码是在WiFi的工作参数上传图片使用AFnetwork 3多部分获取零参数
NSString *URLString = [NSString stringWithFormat:@"%@%@",BASEURL,urlID]; // some URL
NSLog(@"URL : %@ Parameters %@",URLString,postVars);
AFHTTPSessionManager *uploadManager = [AFHTTPSessionManager manager];
[uploadManager POST:URLString parameters:postVars constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSLog(@"Parameters %@",postVars);
if (profileImageData) {
[formData appendPartWithFileData:profileImageData name:@"photo" fileName:@"myimage.jpg" mimeType:@"image/jpeg"];
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Success %@", responseObject);
if ([[responseObject valueForKey:@"status"]isEqualToString:@"success"]) {
block(responseObject,nil);
}
else{
block(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:AFNETWORK_OFFLINE_MESSAGE])
{
[alertHelper showAlertwithTitle:NOINTERNET_TITLE details:NOINTERNET_DETAILS];
}
block(nil,error);
}];
上传图像,但是蜂窝网络上有时paramenters失踪AFNetwork。我的后端开发人员通知我,在上传图像时不会发送参数。我现在应该做什么。
答
试试这个
如果你的参数是零的,然后PAAS零代替_params。
NSMutableDictionary *_params=[[ NSMutableDictionary alloc]init];
[_params setObject:postsomthingtext.text forKey:@"body"];
[_params setObject:userid forKey:@"user_id"];
NSData *imageData = UIImageJPEGRepresentation(choosedimg,.6);
documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Here is the file path for the image
filepath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"imagename.png"]];
[imageData writeToFile:filepath options:NSDataWritingAtomic error:nil];
AFHTTPRequestOperationManager*operationManager = [AFHTTPRequestOperationManager manager];
operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *operation = [operationManager POST:@"http://ekisansansar.com/application/webservices/Webservicefiles.php" parameters: _params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSError *error;
if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:filepath] name:@"upload_file" fileName:[filepath lastPathComponent] mimeType:@"image/png" error:&error]) {
NSLog(@"error appending part: %@", error);
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"responseObject = %@", result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error = %@", error);
}];
if (!operation) {
NSLog(@"Creation of operation failed.");
}
我总是发送无线参数,它完全到达服务器。但是,当使用移动数据时,我也要求参数,并在服务器端,我得到空参数。这里我发送的参数没有达到3g数据的服务器。你明白了吗? –
在增加请求时间后尝试或在后端侧进行调试后检查,为什么您的参数无法通过移动数据访问服务器 –