Losing extension when use FileReference download

2019-02-22 07:27发布

问题:

I need user download JPEG file from my app, however, when user change the file name the saved file will be downloaded without extension.

For Example: I am using FileReference.download() and set the default filename as "demoPic.jpg" and user's windows system setting control file extension not being shown. So when the dialog opens a download window, only "demoPic" shown as the filename. If user saved file without changing filename, the saved file will be OK. But if user change file name, the download file will be saved without extension. is it possible to add file extension to filename when user forget it by flex code?

回答1:

I have the same problem and was not able to fix it until now. It seems it is a problem with flash + windows. :( sorry to inform but I did not found any fix.

If this can be called a workaround, is somehow to warn users and ask them to place also the extension when they rename the file.



回答2:

This is a known problem with Flash Player. An enhancement request is filed with Adobe. Please visit and vote for it: https://bugs.adobe.com/jira/browse/FP-2014

It does not seem practical to train users not to rename the file. Here is the workaround I use for my app:

  • The problem only happens on Windows with ActiveX plugin. Detect ActiveX with Capabilities.playerType (only ~25% of users in my experience)

  • If ActiveX plugin, fall back to 'navigateToURL()' to bounce the file on your server. Using this function is less desirable than FileReference.download() because it is less controllable (the browser is in charge, rather than your app), but it results in a Save dialog box where the user can rename the file safely.



回答3:

Had same problem. Used CONCAT to manually add extension. It does not appear in the dialog box when saving but DOES save as a text file with a .txt extension when viewed in Windows Explorer. Doesn't seem to work but actually does!

var final_filename:String = filename.concat(".txt");

//Create text to save from text field var newDataFile:TextField = new TextField(); newDataFile.text=myTextField.text; //create file reference to save file var file:FileReference = new FileReference(); file.save(newDataFile.text, final_filename);



回答4:

you just need to use navigateToUrl(ur)

var ur:URLRequest=new URLRequest();
var fr:FileReference=new FileReference();
fr.download(ur,<FILENAME>);

"download" method call fileBrowser and then call servlet . then flex can't catch file extension

but "navigateToUrl" called servlet first then servlet call fileBrowser then html can catch file extension



回答5:

protected function downloadImage():void
            {
                loadImage();
            }    
public function loadImage():void
                {
                    var NowDate:Date=new Date();
                    var dateStr:String=String(NowDate.date)+'-'+String(NowDate.month)+'-'+
                        String(NowDate.fullYear)+' '+String(NowDate.hours)+'-'+String(NowDate.minutes)+'-'+String(NowDate.seconds);
                    var file:File= File.desktopDirectory.resolvePath('image_'+dateStr);;
                    file.addEventListener(Event.SELECT,onSelect);
                    file.browseForSave("Save image");

                }
                public var filePath:String='';
                public var fileName:String='';
                public function onSelect(event:Event):void
                {
                    var loader:Loader = new Loader();
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadcompleteHandler);
                    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioError);
                    loader.load(new URLRequest(img_path));
                    fileName=event.target.name;
                    filePath=event.target.url;
                }
                private var _bitmapData:BitmapData;
                public function loadcompleteHandler(event:Event):void
                {
                    var loaderInfo:LoaderInfo = event.target as LoaderInfo;
                    var bitmap:Bitmap = loaderInfo.content as Bitmap;
                    _bitmapData = bitmap.bitmapData;
                    var imgByteArray:ByteArray;
                    var jpegEncoder:JPEGEncoder = new JPEGEncoder(80);
                    imgByteArray = jpegEncoder.encode(_bitmapData);
                    var file:File = new File(filePath+'.jpg');
                    var fileStream:FileStream = new FileStream();
                    fileStream.open(file, FileMode.WRITE);
                    fileStream.writeBytes(imgByteArray);
                    fileStream.close();
                    pop.status_text("Photo Downloaded Successfully",1);
                }