Flex正在写入流时文件已损坏

时间:2012-08-10 03:29:17

标签: flex download

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

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

例如,可以显示this image。但功能是通过中断来下载和写入。

如何修复?

1 个答案:

答案 0 :(得分: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&二进制的。