0

我在主机上获取文件内容并通过以下函数保存文件:

protected function fileDownload(fileName:String):void
{
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, onCompleteFileDownload);
    loader.load(new URLRequest("http://samed.us/"+fileName));
}
private function onCompleteFileDownload(e:Event):void
{
    saveFile("test.jpg",URLLoader(e.target).data);
}
private var Depo:File = File.documentsDirectory.resolvePath("testFiles");
private function saveFile(fileName:String, content:String):void
{
    var file:* = Depo.resolvePath(fileName);
    var stream:FileStream = new FileStream()
    stream.open(file, FileMode.WRITE);
    stream.writeUTFBytes(content);
    stream.close();
}

例如,可以显示此图像。但是功能是通过中断下载和写入。

怎么修?

4

1 回答 1

0

我发现了问题。修复功能:

protected function fileDownload(fileName:String):void
{
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, onCompleteFileDownload);
    loader.dataFormat=URLLoaderDataFormat.BINARY;
    loader.load(new URLRequest("http://samed.us/"+fileName));
}
private function onCompleteFileDownload(e:Event):void
{
    saveFile("test.jpg",(e.target).data);
}

private var Depo:File = File.documentsDirectory.resolvePath("testFiles");
private function saveFile(fileName:String, content:*):void
{
    var file:* = Depo.resolvePath(fileName);
    var stream:FileStream = new FileStream()
    stream.open(file, FileMode.WRITE);
    stream.writeBytes(content);
    stream.close();
}

问题的根源:UTF & Binary。

于 2012-08-10T09:54:39.950 回答