如何从URL中下载文件(iOS / Cocoa SDK),其中文件名不是URL的一部分 [英] How to download a file (iOS/Cocoa SDK) from URL where file name is not the part of URL

查看:233
本文介绍了如何从URL中下载文件(iOS / Cocoa SDK),其中文件名不是URL的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用NSUrl下载文件,其中文件名不可用或不是请求URL的一部分。我一直在看ASIHTTPRequest(http://allseeing-i.com/ASIHTTPRequest/)库。它有

I was wondering on how to download a file using NSUrl where the filename is not available or is not part of the request URL. I have been looking at ASIHTTPRequest (http://allseeing-i.com/ASIHTTPRequest/) library. It has

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"];

但是要点是你需要知道文件名。例如,如果您将网址(不是真实网址) http://some-website/foo.pdf

But the point is you need to know the file name here. For example if you put the URL (not real URL) http://some-website/foo.pdf

它下载一个文件foo.pdf,我想保存它。任何想法谢谢。

It downloads a file foo.pdf and I want to save it like that. Any ideas thanks.

推荐答案

在NSURLConnection委托回调 - (void)connection:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)response ,您可以从 suggestedFileName library / ios /#documentation / cocoa / Reference / Foundation / Classes / NSURLResponse_Class / Reference / Reference.html#// apple_ref / occ / cl / NSURLResponse> NSURLResponse a>。如果你不知道一个名字,那么你需要为你的文件提出另一个名字。此时,您可以在开始保存数据之前指定下载路径。

In the NSURLConnection delegate callback - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response you can retrieve the suggestedFileName from the NSURLResponse. If you get unknown for a name then you need to come up with another name for your file. At this point you can specify your download path before you start saving data.


建议的档案名称

suggestedFilename

传回回应资料的建议
档案。

Returns a suggested filename for the response data.


  • (NSString *)suggestFilename

返回值


响应的建议文件名数据。

A suggested filename for the response data.

讨论

此方法尝试创建
a文件在
中使用以下命令:

The method tries to create a filename using the following, in order:


  1. 使用内容
    处置标头指定的文件名。

  1. A filename specified using the content disposition header.

URL的最后一个路径
组件。

The last path component of the URL.


网址的主机。

The host of the URL.

如果网址主机不能
转换为有效的档案名称,
filenameunknown。

If the host of URL can't be converted to a valid filename, the filename "unknown" is used.

在大多数情况下,此方法会根据
MIME类型附加
正确的文件扩展名。这个方法将总是
返回一个有效的文件名而不管
是否将资源保存到磁盘

In most cases, this method appends the proper file extension based on the MIME type. This method will always return a valid filename regardless of whether or not the resource is saved to disk.

可用性

以后可在iOS 2.0和
中使用。在NSURLResponse.h中声明

Available in iOS 2.0 and later. Declared In NSURLResponse.h

这篇关于如何从URL中下载文件(iOS / Cocoa SDK),其中文件名不是URL的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆