简体   繁体   English

如何使用Adobe Flex写入多个文件

[英]How to write multiple files using adobe flex

I have a string array where each object has comma separated and new line delimiters. 我有一个字符串数组,其中每个对象都有逗号分隔和换行符。 Now i want to write these string objects to multiple CSV files. 现在,我想将这些字符串对象写入多个CSV文件。 But I am able to write only one CSV file as below:- 但是我只能写一个CSV文件,如下所示:

        /**
         * callback method for exportCollection, results are in format of Map<String, String>
         **/
        protected function exportListResultHandler(event:ResultEvent):void {
            if (event.result != null )
            {
                for (var key:* in event.result) {
                    var value:String=event.result[key];
                    var fileName:String=key;
                    exportStringToFile(value.toString(), fileName);
                }
            }
        }

        private function exportStringToFile(stringToExport:String, filename:String=null):void
        {               
            //file refernce for import/export functionality. 
            var fileRef:FileReference = new FileReference(); 

            try{
                fileRef.addEventListener(Event.COMPLETE, finishUpload);            
                fileRef.addEventListener(IOErrorEvent.IO_ERROR, handleError);
                fileRef.save(stringToExport, filename);                
            }catch (error:Error){
                logger.error("Unable to save file. Error: ", error);  

        }

Mine existing codes do not allow me to use File class from flash.filesystem package. 我现有的代码不允许我使用flash.filesystem包中的File类。 Even arrays of FileReference did not helped me. 甚至FileReference数组也无济于事。 I want to save multiple file at one location. 我想在一个位置保存多个文件。

I am new to Adobe flex and flash builder, Any help would be great. 我是Adobe Flex和Flash Builder的新手,任何帮助都会很棒。 Thanks aa lot 非常感谢

If you're create AIR application? 如果要创建AIR应用程序? If yes why not using FileStream to save String to file? 如果是,为什么不使用FileStream将String保存到文件?

var stream:FileStream = new FileStream;
stream.open (targetPath, FileMode.WRITE);
stream.writeUTFBytes (s);
stream.close();

Using FileReference to save file will open dialog box (security thing) and will annoying if you want to save multiple file 使用FileReference保存文件将打开对话框(安全性),如果要保存多个文件,则会很烦人

But if you working for non-AIR application / browser (web) based application other option is you can saved all files using server side code then download it on client. 但是,如果您为非AIR应用程序/基于浏览器(网络)的应用程序工作,则可以选择使用服务器端代码保存所有文件,然后将其下载到客户端。 Of course this option will make your server do extra jobs especially for high traffics 当然,此选项将使您的服务器完成额外的工作,尤其是对于高流量

Flash player wont allow you to save multiple file in a loop. Flash Player不允许您循环保存多个文件。 FP in browser have to open a file saving dialogue box to save the file. 浏览器中的FP必须打开文件保存对话框以保存文件。 In the given scenario, simple and elegant way is to create a zip file on client side using nochump library and add each csv inside and then save that zip file on disk using browser flash player. 在给定方案中,简单而优雅的方法是使用nochump库在客户端创建一个zip文件,并在其中添加每个csv,然后使用浏览器Flash Player将该zip文件保存在磁盘上。 A code example is following 下面的代码示例

var zipOut:ZipOutput=   new ZipOutput();
var ze1:ZipEntry    =   new ZipEntry("csv.csv");
zipOut.putNextEntry(ze1);
zipOut.write(byte_array_to_your_csv_string);
zipOut.closeEntry();

then ask the player to save this zip file. 然后要求播放器保存该zip文件。

You can download nochump AS3 from https://github.com/mazerte/openflow-as3/tree/master/src/nochump/util/zip 您可以从https://github.com/mazerte/openflow-as3/tree/master/src/nochump/util/zip下载nochump AS3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM