如何检查文件是否存在于特定的 URL? [英] How to check if a file exists at particular URL?
问题描述
如何检查网站上是否存在文件?我将 NSURLConnection
与我的 NSURLRequest
和一个 NSMutableData
对象一起使用来存储 didReceiveData:
委托方法中返回的内容.在 connectionDidFinishingLoading:
方法中,我将 NSMutableData
对象保存到文件系统.都好.除了:如果该文件在网站上不存在,我的代码仍然会运行,获取数据并保存文件.
How do I check if a file exists on a web site? I am using NSURLConnection
with my NSURLRequest
and an NSMutableData
object to store what comes back in the didReceiveData:
delegate method. In the connectionDidFinishingLoading:
method I then save the NSMutableData
object to the file system. All good. Except: if the file does not exist at the website, my code still runs, gets data and saves a file.
如何在我提出下载请求之前检查文件是否存在?
How can I check the file is there before I make download request?
推荐答案
实现connection:didReceiveResponse:
,会在connection:didReceiveData:
之前调用.
响应应该是一个 NSHTTPURLResponse
对象——假设您正在发出一个 HTTP 请求.因此,您可以检查 [response statusCode] == 404
以确定文件是否存在.
The response should be an NSHTTPURLResponse
object — suppose you're issuing an HTTP request. You can therefore check [response statusCode] == 404
to determine if the file exists or not.
另请参阅检查 NSURL 是否返回 404.
这篇关于如何检查文件是否存在于特定的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!