我正在努力下载文件,它似乎工作,因为我检查了模拟器的文件夹确实我确实下载了文件。这是我的下载方法:
NSString *JSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
NSDictionary *data = @{@"data": JSON};
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"download"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *op = [manager POST:server
parameters:data
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"successful download to %@", path);
[blockView removeFromSuperview];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:webView];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[blockView removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Download failed!"
message:error.localizedDescription
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
现在我有两个问题:
修改
响应总是application/octet-stream
EDIT2 下面的解决方案 - 但我仍然想知道如何通过邮件检查/显示/发送/使用真实设备上的下载文件?
TIA
答案 0 :(得分:0)
所以我下载,保存并在UIWebView
成功显示:)我把它放在请求操作的成功块中:
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[[operation.response.allHeaderFields objectForKey:@"Content-Disposition"] substringFromIndex:22]];
path = [path substringToIndex:path.length-1];
[operation.responseData writeToFile:path atomically:YES];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:webView];