2012-04-02 82 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