2012-04-02 36 views
0

所以我試圖將用戶選擇的文件複製到AIR應用程序存儲目錄中,但是當我嘗試解析存儲目錄中的路徑時,我得到:錯誤#3002寫入應用程序存儲目錄

錯誤#3002:文件或目錄存在。

即使沒有可能的方式,該文件已經存在(我很確定沒有「qqqqq.txt」那裏)我得到這個。

我的代碼如下所示:

var saveFile:File = File.desktopDirectory; 

saveFile.browseForOpen("Open File");  
saveFile.addEventListener(Event.SELECT, function(e:Event):void 
    { 
      txt.text = saveFile.nativePath; 
      var destination:File = File.applicationStorageDirectory; 
      destination = destination.resolvePath("files/moreInfo.txt"); 
       try 
       { 
       saveFile.copyTo(destination, true); 
       trace("Saved Attachment Success: "+destination.toString()); 
       } 
       catch (error:Error) 
       { trace("Error: "+ error.message); } 
    }); 

錯誤是扔在那裏我嘗試設置目的地爲applicationStorageDirectory線,但我不知道爲什麼。

任何想法?

編輯:所以我評論了一切低於var destination:File = File.applicationStorageDirectory;,它仍然會引發錯誤。

+0

var destination:File = File.applicationStorageDirectory;'沒有什麼問題。我有一種感覺,那不是你的錯誤的來源。 – 2012-04-02 19:57:19

+0

您是否在應用程序包中創建了文件目錄? – francis 2012-04-02 22:13:59

+0

糾正我,如果我錯了,但你可以添加一個文件夾到appStorageDir? – francis 2012-04-03 01:07:06

回答

1

當您調用resolvePath時,將使用resolvePath的返回值 - 它不會將分辨率應用於當前的File對象。

例子:

var prefsFile:File = File.applicationStorageDirectory; 
prefsFile = prefsFile.resolvePath("preferences.xml"); 

而且,看來你的目標文件永遠是:「文件/ moreInfo.txt」

下面是一個示例實現,如果你只是想選擇一個文件被複制到應用程序存儲目錄。

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"> 

    <fx:Script> 
     <![CDATA[ 
      /** 
      * Selected file chosen by user. 
      */ 
      protected var sourceFile:File; 


      /** 
      * Function to choose a source file to be copied to 
      * the application storage directory. 
      */ 
      protected function saveButtonClickHandler(event:MouseEvent):void 
      { 
       var fileTypes:FileFilter = new FileFilter("Text File (*.txt)", "*.txt;"); 
       var allTypes:Array = new Array(fileTypes); 

       // default selection to user's desktop 
       sourceFile = File.desktopDirectory; 

       // setup listeners 
       sourceFile.addEventListener(Event.SELECT, fileSelectHandler); 

       // browse for file 
       try 
       { 
        sourceFile.browse(allTypes); 
       } 
       catch (error:Error) 
       { 
        trace("error selecting file to be copied."); 
       } 
      } 

      /** 
      * Called upon selection of file. 
      */ 
      protected function fileSelectHandler(event:Event):void 
      { 
       try 
       { 
        // selected source path 
        trace("Source path: " + sourceFile.nativePath); 

        // set destination path 
        var destinationFile:File = File.applicationStorageDirectory.resolvePath("files/" + sourceFile.name); 
        trace("Destination path: " + destinationFile.nativePath); 

        // check if destination file path already exists and copy 
        if (destinationFile.exists) 
         trace("source file already copied to application storage directory."); 
        else 
         sourceFile.copyTo(destinationFile); 
       } 
       catch (error:Error) 
       { 
        trace("failed to copy source file to destination path."); 
       } 

       // remove listeners 
       sourceFile.removeEventListener(Event.SELECT, fileSelectHandler); 
      } 
     ]]> 
    </fx:Script> 


    <s:Button label="Save As..." 
       click="saveButtonClickHandler(event)" 
       width="100%" 
       height="100%" /> 


</s:WindowedApplication> 
+0

編輯修改。但由於錯誤高於該線,因此不會改變任何內容。 – korukyu 2012-04-03 21:21:55

+0

如果您只是試圖將選定的文件複製到應用程序存儲目錄,我已經包含一個示例。 – 2012-04-03 21:56:17

+0

我嘗試了你的建議,但它仍然在'var destinationFile:File = File.applicationStorageDirectory'上拋出錯誤。我的權限或其他內容肯定有問題。我只是將它移到我的桌面上,因爲它在那裏工作得很好。謝謝,不過。 – korukyu 2012-04-04 20:24:42

相關問題