Save String To File


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private var fileToSave:File = File.documentsDirectory;
  2.  
  3. // click handler
  4. protected function buttonExportToCSV_clickHandler(event:MouseEvent):void
  5. {
  6. fileToSave.addEventListener(Event.SELECT, fileToSave_selectedHandler);
  7. fileToSave.browseForSave("Save .CSV");
  8. }
  9.  
  10. protected function fileToSave_selectedHandler(event:Event):void
  11. {
  12. var nativePath:String;
  13. if( fileToSave.extension == null ) {
  14. nativePath = fileToSave.nativePath + ".csv";
  15. }
  16. else {
  17. nativePath = fileToSave.nativePath.substr(0, fileToSave.nativePath.lastIndexOf(fileToSave.extension)) + ".csv";
  18. }
  19.  
  20. var file:File = new File(nativePath);
  21. if( file.exists ) {
  22. Alert.show("There already exists a .csv file of that name", "Exporting Aborted");
  23. }
  24.  
  25. var stream:FileStream = new FileStream();
  26. stream.open(file.FileMode.WRITE);
  27. stream.writeUTFBytes(value); // value being the clipboard content
  28. stream.close();
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.