korukyu
korukyu

Reputation: 33

Error #3002 in writing to application storage directory

So I'm trying to copy a file selected by the user into the AIR application storage directory, but when I attempt to resolve the path in the storage directory, I get:

Error #3002: File or directory exists.

I get this even when there is no possible way that the file already exists (I am pretty sure there is no "qqqqq.txt" there).

My code looks like this:

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); } 
    });

The error is thrown on the line where I try to set the destination as the applicationStorageDirectory, but I have no idea why.

Any ideas?

Edit: So I commented out everything below var destination:File = File.applicationStorageDirectory; and it STILL throws the error.

Upvotes: 0

Views: 1183

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

When you call resolvePath, you use the return value of resolvePath - it does not apply the resolution to the current File object.

Example:

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

As well, it seems your destination file is always: "files/moreInfo.txt"

Here's an example implementation, if you're just trying to select a file to be copied to the application storage directory.

<?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>

Upvotes: 1

Related Questions