as3文件下载限制?

时间:2013-03-21 11:10:08

标签: actionscript-3 flash

我在我的网站上下载了一个flash文件下载pdf文件。

var myfileReference:FileReference = new FileReference();
down_mc.visible = false;
down_comp.visible = false;

var myRequest:URLRequest = new URLRequest("GEORGIA INCORPORATED.pdf");
myfileReference.addEventListener(IOErrorEvent.IO_ERROR, ioError);
output_txt.text = "";

function ioError(event:ErrorEvent):void {
output_txt.text = "Sorry that there is an IO error during the file downloading. The error is:" + "\n" + event;

}

myfileReference.addEventListener(ProgressEvent.PROGRESS, fileDownloadProgress);

function fileDownloadProgress(event:ProgressEvent):void {

    down_mc.visible = true;
}
myfileReference.addEventListener(Event.COMPLETE, fileDownloadCompleted);

function fileDownloadCompleted(evt:Event):void {
    down_mc.visible = false;
    down_comp.visible = true;

}

function downloadFile (event:MouseEvent):void {

    try {
        myfileReference.download(myRequest);
    } catch (error:SecurityError) {

        downloading. The error is:" + "\n" + error; 

    } catch (error:IllegalOperationError) {

        downloading. The error is:" + "\n" + error; 
    }
    }
b1.addEventListener(MouseEvent.CLICK, downloadFile);

问题是有些人想要更改他们正在下载的文件的名称并更改扩展名.pdf,从而使文件无法使用。有没有办法限制客户改变扩展名?

1 个答案:

答案 0 :(得分:0)

在纯as3中,您无法强制用户在特定扩展下保存文件。唯一的方法就是使用Air让你通过FileStream对象控制它(你可以选择文件名)。

如果您想使用Air,那么您可以这样做:

// Assuming you get your pdf raw data
var pdfData : ByteArray;

var f : File = File.desktopDirectory.resolvePath("myPdf.pdf");

// When user have select a file
f.addEventListener(Event.SELECT, function(e:Event):void{
    var targetFile : File = e.target as File;

    // Check if the selected file have pdf extension
    if(targetFile.nativePath.substr(targetFile.nativePath.length - 4) != ".pdf")
        // If not, add it
        targetFile = new File(targetFile.nativePath + ".pdf");

    // Save the file
    var fs : FileStream = new FileStream();
    fs.open(targetFile, FileMode.WRITE);
    fs.writeBytes(pdfData);
    fs.close();
});

// Ask user to save a file (by default on user desktop)
f.browseForSave("Save PDF");