用AFN上传本地文件的时候遇到的本地文件路径的问题
//MARK:2.66 H5图片上传接口
- (void)uploadImageWithImageFile:(UIImage *)imageFile imageName:(NSString *)imageName complete:(void(^)(id model))completeBlock failure:(void(^)())failureBlock {
NSString *url = SERVER_INTERFACE_HTML_UPLOADIMAGE;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:0];
// [parameters setValue:imageFile forKey:@"imagefile"];
[parameters setValue:imageName forKey:@"imagename"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
WEAKSELF
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// NSURL *fileUrl = [NSURL URLWithString:@"/Users/liliansi/Desktop/testshot.png"];
// [formData appendPartWithFileURL:[NSURL URLWithString:fileUrl] name:@"imagefile" fileName:imageName mimeType:@"image/jpg" error:NULL];
// [formData appendPartWithFileData:[NSData dataWithContentsOfFile:fileUrl] name:@"imagefile" fileName:imageName mimeType:@"image/jpg"];
VcUser *user = [[VcUserManager sharedInstance] currentUser];
NSString *path = [[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"users"] stringByAppendingPathComponent:user.userID.stringValue] stringByAppendingPathComponent:self.productid.stringValue];
BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSAssert(bo,@"创建目录失败");
NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
// NSString *path1 = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSBundle *bundle = [NSBundle bundleWithPath:path];
// NSURL *fileURL = [bundle URLForResource:imageName withExtension:nil];
// NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:imageName];
// NSURL *fileURL = [NSURL URLWithString:filePath];
NSLog(@"\npath: %@\nimageName: %@",path,imageName);
NSLog(@"\nfileURL: %@",fileURL);
[formData appendPartWithFileURL:fileURL name:@"imagefile" error:NULL];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", [operation responseString]);
NSDictionary *dict = (NSDictionary *)responseObject;
NSString* code = [dict objectForKey:@"code"];
if (code == nil || ![code isEqualToString:SERVER_RESPONSE_CODE_SUCCESS]) {
NSString* errDesc = [dict objectForKey:@"desc"];
[weakSelf.view makeToast:errDesc duration:2.0 position:@"bottom"];
if (failureBlock) {
failureBlock();
}
return;
}
NSDictionary *imageinfo = [dict objectForKey:@"imageinfo"];
if (imageinfo && ![imageinfo isKindOfClass:[NSNull class]]) {
VcImageModel *pictureModel = [[VcImageModel alloc]initWithAttributesDictionary:imageinfo];
if (completeBlock) {
completeBlock(pictureModel);
}
}
NSLog(@"上传完成");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败->%@", error);
NSLog(@"错误 %@", error.localizedDescription);
if (failureBlock) {
failureBlock();
}
}];
}
问题以及总结:
初始化URL,其中 filePath 是一个本地的文件路径。
NSURL *appURL = [NSURL URLWithString:filePath];
在真机上可以初始化appURL,但模拟器上appURL为nil;
正确初始化一个本地文件的URL,应使用下边的方法,真机和模拟器都没问题
NSURL *appURL = [NSURL fileURLWithPath:url];
做实验:
NSString *str=@”http://www.9yunping.com/index“;
NSURL *url1=[NSURL fileURLWithPath:str];
NSURL *url2=[NSURL URLWithString:str];
NSLog(@”url1=%@”,url1);
NSLog(@”url2=%@”,url2);
输出结果为:
url1=http:/www.9yunping.com/index – file:///
url2=http://www.9yunping.com/index
通过上面的输出结果可知:
fileURLWithPath是将str转化为文件路径,可以自动的去掉“/”。而URLWithString紧紧是将url2转化成NSURL类型,对于它的内容没有任何的改变。