所以我试图将用户选择的文件复制到AIR应用程序存储目录中,但是当我尝试解析存储目录中的路径时,我得到:
错误#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 :(得分:1)
调用resolvePath时,使用resolvePath的返回值 - 它不会将解析应用于当前的File对象。
示例:
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
同样,您的目标文件似乎总是:“files / 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>