2013-03-21 93 views
1

我在我的網站上下載了一個pdf文件的Flash下載。as3文件下載限制?

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

我認爲你不能從節約與另一部分下載的文件限制用戶。它只是文件名的一部分,特別是當涉及到Linux時,甚至在Windows中它不再起到關鍵作用,在長名稱引入後,名稱+ ext被合併到一個數據字段中。 – Vesper 2013-03-21 11:14:09

回答

0

在純as3中,不能強制用戶在特定擴展名下保存文件。唯一的方法是使用Air,通過FileStream對象控制它(可以選擇文件名)。

如果你想用空氣做,那麼你可以做:

// 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"); 
+0

謝謝,老闆決定讓他們改變任何他們想要的,如果文件不起作用,這將是他們自己該死的錯。謝謝,但我會給它一個空氣:) – Guram 2013-03-25 09:59:32