检查 URL 文件是否存在
我想知道如何在不先下载数据的情况下检查服务器上是否存在文件。
我有大约 30 个不同的对象,其中一些连接到服务器上的电影。目前,我使用 NSData 来控制 URL 是否存在,然后显示电影,如果不存在,则提醒用户该对象没有视频。我目前使用的代码:
NSString *fPath = [[NSString alloc] initWithFormat:@"http://www.myserver/%@", [rows idNr]];
NSURL *videoURL = [NSURL URLWithString:fPath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
url = [NSURL URLWithString:fPath];
[fPath release];
if (videoData) {
[self performSelectorOnMainThread:@selector(playVideo:) withObject:url waitUntilDone:NO];
} else {
NSLog(@"videodata false");
errorLabel.hidden = NO;
activityView.hidden = YES;
}
“rows idNr”是对象的名称。这个方法正在做我想要的事情,但问题是,使用 NSData 它首先“下载”文件,当 URL 被验证为文件时,电影会在电影播放器中再次加载。这意味着加载文件需要两倍的时间。
建议?
I wonder how I can check if a file exist on a server or not, without downloading the data first.
I have around 30 different objects and some of them is connected to a movie on a server. At the moment I use NSData to control if the the URL exist, and then shows the movie, or if it doesn't and then alerts the user that there is no video for that object. The code I use for the moment:
NSString *fPath = [[NSString alloc] initWithFormat:@"http://www.myserver/%@", [rows idNr]];
NSURL *videoURL = [NSURL URLWithString:fPath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
url = [NSURL URLWithString:fPath];
[fPath release];
if (videoData) {
[self performSelectorOnMainThread:@selector(playVideo:) withObject:url waitUntilDone:NO];
} else {
NSLog(@"videodata false");
errorLabel.hidden = NO;
activityView.hidden = YES;
}
"rows idNr" is the name of the object. This method is doing what I want, but the problem is that with NSData it first "downloading" the file, and when the URL is validated as a file, the movie is loading once again in the movieplayer. This means that it takes twice as long to load the file.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我花了一段时间才找到有关此主题的先前问题之一的答案< /a>.引用:
这样您就可以知道该文件是否存在,如果存在则不会全部下载。
It took me a while to dig out my answer to one of the previous questions on this topic. Quote:
This way you’ll know if the file exists and won’t download it all if it does.
使用所需的 url 请求创建一个 URLConnecion 对象,并将
NSURLConnectionDelegate
添加到 .h 文件中,就像我想检查“yoururl”是否存在一样,然后你需要做的是,然后你可以在
NSURLConnectionDelegate
的委托函数中跟踪http状态码,还可以找到各种状态码此处。
Make an URLConnecion object with desired url request and add
NSURLConnectionDelegate
into .h file like I want to check "yoururl" is exist or not then you need to do isand then you can track http status code in delegate function of
NSURLConnectionDelegate
You can also find various status code here.
以下是已接受答案的代码(为了您的方便):
如何拨打电话
Here's the code for the accepted answer (for your convenience):
How to make call
您可以通过 FTP 服务器使用 SIZE 命令检查文件的大小来完成此操作。如果文件大小为零,则该文件根本不存在。
请查看此处了解如何执行此操作。
当然,您也可以通过使用
NSURLRequest
和NSURLConnection
来完成此操作,检查状态为 200(成功)或 404(失败)。 404 状态不一定是文件不存在,也可能是文件无法检索。You could do this by checking the size of the file via an FTP server, using the SIZE command. If the file size is zero then the file simply do not exist.
Check here on how to do this.
You could of course also do this by using a
NSURLRequest
withNSURLConnection
, checking for the status to be either 200 (success) or 404 (failed). The 404 status doesn't have to be that the file doesn't exist though, it could also be that the file just couldn't be retrieved.