2011-03-31 47 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中播放。