Flex从远程服务器下载文件

时间:2012-02-14 00:58:05

标签: actionscript-3 flex

使用Air,我尝试从远程服务器加载文件并使用默认应用程序打开文件。

private function getRemoteCourr(PathFichier:String, FileName:String):void  {
    urlStream = new URLStream();
    fileData  = new ByteArray();
    var urlRemoteCourr:String = new urlManager().urlCourriersPat();

    var PathFichierGlob: String = urlRemoteCourr+PathFichier;
    var urlReq:URLRequest = new URLRequest(urlRemoteCourr);
    urlStream.addEventListener(Event.COMPLETE, function(e:*):void {fileLoaded(e,FileName)});
    urlStream.load(urlReq);
}

private function fileLoaded(event:Event, PathFichier:String):void  {
    urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
    var file:File = File.documentsDirectory.resolvePath(PathFichier);
    var fileStream:FileStream = new FileStream();
    fileStream.openAsync(file, FileMode.WRITE);
    fileStream.writeBytes(fileData, 0, fileData.length);
    fileStream.close();

    file.openWithDefaultApplication();
}

使用这部分代码,文件目录上正在创建文件(我想要),但内容不是原始文件内容。 对于word文档,新内容为

Index of /MyApp/LinkMySQL/Customer/Courriers

Name
Last modified
Size
Description


Parent Directory 
    -    

0/ 
13-Feb-2012 20:24   

而不是带有图像和文字的word文档。

2 个答案:

答案 0 :(得分:1)

在下面找到解决方案

public class fMoveCopy
{

    private var urlStream:URLStream;  
    private var fileData:ByteArray = new ByteArray(); 
    private var fileName:String;
    private var fileLocal:File;

    public function fMoveCopy()
    {
    }


    public function getFileRemote(pathRemote:String, fileNameR:String):void  
    {  
        urlStream = new URLStream();
        var urlReq:URLRequest = new URLRequest(pathRemote);  
        urlStream.addEventListener(Event.COMPLETE, loaded);  
        urlStream.load (urlReq);  
        fileName = fileNameR;
    }  

    private function loaded(event:Event):void  
    {  
        urlStream.readBytes (fileData, 0, urlStream.bytesAvailable);  
        resolveFile(fileName);  
    }  

    private function resolveFile(pathLocal:String):void  
    {  
        fileLocal = File.applicationStorageDirectory.resolvePath(pathLocal);  
        var fileStream:FileStream = new FileStream();  
        fileStream.addEventListener(Event.CLOSE, fileClosed);  
        fileStream.openAsync(fileLocal, FileMode.WRITE);  
        fileStream.writeBytes(fileData, 0, fileData.length);  
        fileStream.close();  
    }  

    private function fileClosed(event:Event):void  
    {  

        fileLocal.openWithDefaultApplication();
    }  




}

答案 1 :(得分:1)

唯一可能发生的事情是您正在下载目录列表而不是实际文件。例如http://www.myserver.com/files代替http://www.myserver.com/files/myDocument.doc

您可以在开始下载之前添加支票

但是,如果你查看代码,你会看到

private function getRemoteCourr(PathFichier:String, FileName:String):void  {
    urlStream = new URLStream();
    fileData  = new ByteArray();
    var urlRemoteCourr:String = new urlManager().urlCourriersPat();

    var PathFichierGlob: String = urlRemoteCourr+PathFichier;
    //var urlReq:URLRequest = new URLRequest(urlRemoteCourr);
    //!!!!REQUESTING ONLY urlRemoteCourr
    //INSTEAD DO THIS:
    var urlReq:URLRequest = new URLRequest(PathFichierGlob);
    urlStream.addEventListener(Event.COMPLETE, function(e:*):void {fileLoaded(e,FileName)});
    urlStream.load(urlReq);
}

哦,就像传递说明一样,AS3变量名称不以大写字母开头(就像你使用过的那样)。班级名称。这就是惯例。