2011-03-31 44 views
3

考虑下面的代码:ASIHTTPRequest setDownloadDestinationPath不写入文件

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) 

将有问题的URL给我发送QuickTime影片(我使用Ruby的send_data File.read(“#{file_path} .MOV”),:disposition =>'inline',:type =>“video/quicktime”)。

如您所见,目录中没有文件!帮帮我!

+0

另外只是要注意,我试着用服务器的直接文件(http://192.168.2.71:3000/fake_recordings/encounters/2/complaint.MOV),而不需要一个控制器,这有相同的问题。即使我可以在两种情况下都使用我的网络浏览器正确打开文件。 – mattvv 2011-03-31 01:44:23

+0

尝试添加失败回调并查看它是否被调用? – Nevin 2011-03-31 01:54:29

+0

有效的点,我做到了,并得到这个: 2011-03-30 19:01:01.957 VideoCapture [246:707]错误:无法将文件从'/ private/var/mobile/Applications/AE4B3091-3726- 4FAE-B861-C4AE3616E743/tmp/6A81D988-74EF-4085-AB1F-8EFF11008736-246-0000000F08A0C88D'至'/var/mobile/Applications/AE4B3091-3726-4FAE-B861-C4AE3616E743/Documents/complaint.MOV' – mattvv 2011-03-31 02:01:27

回答

4

在iOS NSHomeDirectory()下,附加路径不是到达Document目录的适当方式,可能是挂断你的东西。相反,沿着这些线使用的东西:

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString *mediaPath = [documentDirectory stringByAppendingPathComponent:@"complaint.MOV"]; 
+0

试过这个解决方案,遗憾的是仍然无法下载该文件。 2011-03-31 11:19:19.670 VideoCapture [1047:707]文件名是/var/mobile/Applications/AE4B3091-3726-4FAE-B861-C4AE3616E743/Documents/complaint.MOV 2011-03-31 11: 19:19.671 VideoCapture [1047:707]错误:无法从'/ private/var/mobile/Applications/AE4B3091-3726-4FAE-B861-C4AE3616E743/tmp/EBA8412C-16AE-4B57-ACC6-A5C74BD0334E-1047- 000000A15A2CEE06'到'/var/mobile/Applications/AE4B3091-3726-4FAE-B861-C4AE3616E743/Documents/complaint.MOV' 2011-03-31 11:19:19.679 VideoCapture [1047:707]文档目录:(null) – mattvv 2011-03-31 18:20:19

+0

@ user164458这是在设备上还是在模拟器中?这绝对是模拟器中的一条无效路径,我认为它也不适用于他的设备。 – 2011-03-31 19:50:06

1

我有类似的问题。意识到它发生在您将请求设置为self的委托(ASIHTTPRequestDelegate)时。如果你避免这种情况,它可以写入文件。

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中播放。