1

我在我的网站上有一个 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 ,从而使文件无法使用。有什么办法可以阻止客户更改扩展名?

4

1 回答 1

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");
于 2013-03-22T10:31:44.587 回答