我正在开发一个应用程序,我需要将文件上传到某个服务器,并且我正在使用AFNetworking
上传文件。
所有工作正常,除非文件大小很短,如2mb,如果超过此值,服务器会回复我错误状态代码500并且不上传文件。
下面是我上传的代码。
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:lang forHTTPHeaderField:@"Accept-Language"];
[request setValue:authVal forHTTPHeaderField:@"authorization"];
[request setValue:[headerParams objectForKey:@"x-file-name"] forHTTPHeaderField:@"x-file-name"];
[request setValue:[headerParams objectForKey:@"x-convert-document"] forHTTPHeaderField:@"x-convert-document"];
[request setValue:[headerParams objectForKey:@"x-source"] forHTTPHeaderField:@"x-source"];
//convert parameters in to json data
if ([params isKindOfClass:[NSDictionary class]]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted
error:&error];
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
[request setURL:[NSURL URLWithString:action]];
[request setTimeoutInterval:200.0];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:imageData];
NSLog(@"File size is : %.2f MB",(float)imageData.length/1024.0f/1024.0f);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger statusCode = [operation.response statusCode];
NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];
successBlock(responseObject, statusObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
代码行[request setHTTPBody:imageData];
正在设置图片的NSData
。
同样的事情适用于Android及其工作正常,但他们使用流,首先将文件写入物理路径,然后使用流上传。这可以使用AFNetworking
吗?或者我也可以通过其他方法实现这一目标吗?
寻找建议。
由于
答案 0 :(得分:0)
注意: - 我刚刚使用AFNetworking 3.0实现了图片上传服务,
- >这里kBaseURL表示服务器的基本URL
- > serviceName表示服务名称。
- > dictParams表示根据需要提供参数,否则为nil。
- >图片意味着传递您的图片。
注意: - 此代码用NSObject类编写,我们已在项目中使用。
+ (void)requestPostUrlWithImage: (NSString *)serviceName parameters:(NSDictionary *)dictParams image:(UIImage *)image success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {
NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[SVProgressHUD show];
NSData *fileData = image?UIImageJPEGRepresentation(image, 0.5):nil;
NSError *error;
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:strService parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(fileData){
[formData appendPartWithFileData:fileData
name:@"image"
fileName:@"img.jpeg"
mimeType:@"multipart/form-data"];
}
} error:&error];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"Wrote %f", uploadProgress.fractionCompleted);
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
[SVProgressHUD dismiss];
if (error)
{
failure(error);
}
else
{
NSLog(@"POST Response : %@",responseObject);
success(responseObject);
}
}];
[uploadTask resume];
}
- &GT;现在申请我们的项目。
UIImage *imgProfile = //Pass your image.
[WebService requestPostUrlWithImage:@"saveImageWithData" parameters:nil image:imgProfile success:^(NSDictionary *responce) {
NSString *check = [NSString stringWithFormat:@"%@",[responce objectForKey:@"status"]];
if ([check isEqualToString: @"1"]) {
//Image Uploaded.
}
else
{
//Failed to Upload.
}
} failure:^(NSError *error) {
//Error
}];