请考虑以下代码:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.71:3000/ios_file?filename=complaint&folder=encounters&id=3"]];
NSString *mediaPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
mediaPath = [mediaPath stringByAppendingPathComponent:@"complaint.MOV"];
[request setDownloadDestinationPath:mediaPath];
[request startSynchronous];
NSLog(@"Got the file!");
NSURL *theURL = [NSURL URLWithString:mediaPath];
NSLog(@"Time to Play File!");
NSLog(@"Filename is %@", [theURL absoluteString]);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[theURL absoluteString]];
if (fileExists) {
NSLog(@"THE FILE EXISTS ZOMG");
}
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
返回以下内容:
2011-03-30 18:29:46.107 VideoCapture[158:707] Get recording from server
2011-03-30 18:29:46.976 VideoCapture[158:707] Got the file!
2011-03-30 18:29:46.978 VideoCapture[158:707] Time to Play File!
2011-03-30 18:29:46.980 VideoCapture[158:707] Filename is /var/mobile/Applications/AE4B3091-3726-4FAE-B861-C4AE3616E743/Documents/complaint.MOV
2011-03-30 18:29:46.986 VideoCapture[158:707] Documents directory: (null)
转到相关网址会向我发送一部快速电影(我正在使用Ruby的send_data File.read(“#{file_path} .MOV”),:disposition =>'inline',:type =>“视频/ QuickTime的“)。
如您所见,目录中没有文件!救命啊!
答案 0 :(得分:4)
在iOS NSHomeDirectory()
下,附加路径不是到达Document目录的正确方法,可能会让你感到困惑。而是沿着这些方向使用:
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *mediaPath = [documentDirectory stringByAppendingPathComponent:@"complaint.MOV"];
答案 1 :(得分:1)
我遇到了类似的问题。意识到当你为自己的请求设置委托(ASIHTTPRequestDelegate)时会发生这种情况。如果你避免这种情况,可以写文件。
答案 2 :(得分:0)
因为您使用标准ASIHTTPRequest下载文档(即未接收NSData对象中的字节并手动处理),解决此问题的最佳方法如下:
1)实现ASIHTTPRequestDelegate方法 2)在requestFinished方法中,您可以使用NSFileManager手动移动文件 3)从新修改的路径播放电影
以下是如何实现此目的的详细示例。根据需要修改更简洁的简化代码。
// create NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
// grab the current request object's download path, minus the file extension
NSString *directoryPath = [[request downloadDestinationPath] stringByDeletingPathExtension];
// append the desired file extension
NSString *destintionPath = [NSString stringWithFormat:@"%@.mov", directoryPath];
// move the file
[fileManager moveItemAtPath:[request downloadDestinationPath] toPath:destintionPath error:nil];
我可以使用此代码下载QuickTime影片而不会出错。默认情况下,它将下载到downloadDestinationPath,在这种情况下,它将采用GUID.html格式。然后我们将其重命名为GUID.mov,然后发送给MPMoviePlayer播放。