问题
i want to have force download functionality only with Flash AS3, is it possible ?? i tried google but failed. here it is my as3 code......
var file_URLRequest:URLRequest = new URLRequest ("mp3gallery/" + url);
var content_header:URLRequestHeader = new URLRequestHeader("Content-Type: application/force-download");
var attach_header:URLRequestHeader = new URLRequestHeader("Content-Disposition: attachment; filename=abc.mp3");
file_URLRequest.requestHeaders.push(content_header);
file_URLRequest.requestHeaders.push(attach_header);
file_URLRequest.method = URLRequestMethod.POST;
navigateToURL(file_URLRequest, '_blank');
Thank you.
回答1:
This is directly from the docs:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.FileFilter;
public class FileReference_download extends Sprite {
private var downloadURL:URLRequest;
private var fileName:String = "SomeFile.pdf";
private var file:FileReference;
public function FileReference_download() {
downloadURL = new URLRequest();
downloadURL.url = "http://www.[yourDomain].com/SomeFile.pdf";
file = new FileReference();
configureListeners(file);
file.download(downloadURL, fileName);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CANCEL, cancelHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
}
private function cancelHandler(event:Event):void {
trace("cancelHandler: " + event);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + downloadURL.url);
}
}
}
EDIT: this could not work if used this way, because an action of the user (a click) may be required. So you should move the code contained in the constructor to a method (click handler). Of course, in this case btn is the instance name of a movieclip placed on stage, while FileReference_download is the DocumentClass.
public function FileReference_download() {
btn.addEventListener(MouseEvent.MOUSE_DOWN, downloadMyFile);
}
private function downloadMyFile(e:MouseEvent){
downloadURL = new URLRequest();
downloadURL.url = "http://www.[yourDomain].com/SomeFile.pdf";
file = new FileReference();
configureListeners(file);
file.download(downloadURL, fileName);
}
来源:https://stackoverflow.com/questions/8462458/force-download-with-flash-as3