Ios 如何签入下载的文件?
我正在下载文件,它似乎起作用了,因为我检查了模拟器的文件夹,确实下载了文件。以下是我的下载方法:Ios 如何签入下载的文件?,ios,iphone,ios-simulator,afnetworking,Ios,Iphone,Ios Simulator,Afnetworking,我正在下载文件,它似乎起作用了,因为我检查了模拟器的文件夹,确实下载了文件。以下是我的下载方法: NSString *JSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrett
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];
现在我有两个问题:
在下载之前,我知道的都是文档id,所以我不知道它是pdf还是png或者其他格式——我应该做些什么吗?理想情况下,我会添加适当的扩展,但这是已知的下载完成后。。。
我应该如何在模拟器中进行测试,以检查在这种情况下我是否可以读取设备或模拟器上下载的文件?
编辑
响应总是应用程序/八位字节流
编辑2
下面的解决方案-但我仍然想知道如何在真实设备上检查/显示/通过邮件发送/使用下载的文件
tia因此我成功下载、保存并显示在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];
状态数据可能会告诉您文件类型。您所说的状态数据是指?请求状态数据。我不使用那个特定的接口,所以我不知道具体细节,但传输时会返回一个状态字符串,其中的一部分信息通常是作为mime类型的文件类型。它总是应用程序/八位字节流,这就是为什么我试图找出如何正确保存它的原因