AS3 delete a file once it's been accessed via URLLoader

818 views Asked by At

In AS3 in AIR I'm using a URLLoader to load json data from a file for parsing. Then I want to add objects to the data and re-write the same file, using WRITE and not APPEND. The AIR compiler says, essentially "no can do, the file's in use."

Nullifying the data or closing the URLLoader before re-writing or deleting the file don't work.

How do you get control of a file once a URLLoader has loaded it's data?

2

There are 2 answers

0
Craig On

Well, I see that this has been solved by others before. A very slight delay (actually a timer of 0 milliseconds!) is all that’s needed before the delete function to make this problem go away. Interestingly, if the timer is left out of the wait() function and deleteIt() is called from there instead, the problem returns.

import flash.filesystem.*;
import flash.net.URLLoader;

var json:URLLoader

function loadMyJSONData():void 
{
       json = new URLLoader();
       json.addEventListener(Event.COMPLETE, wait);
       json.load(new URLRequest("jsondata.json"));
}


function wait(e)
{
    var timer:Timer = new Timer(0, 1);
    timer.addEventListener(TimerEvent.TIMER, deleteIt);
    timer.start();

    //deleteIt();
}


function deleteIt(e)
{
    var fileToDelete:File = new File();  
    fileToDelete.nativePath = "C:\\AIR Test 2";         
    fileToDelete = fileToDelete.resolvePath("jsondata.json");           

    fileToDelete.deleteFile();
}
0
David Mennenoh On

Use FileStream instead of URLLoader, and you won't need to use a hacky 0ms timer.