Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save file? 1

Status
Not open for further replies.

Cheech

Technical User
Nov 6, 2000
2,933
EU
I have been looking at the code to save a text file from a flash object. I managed to find a script that nearly does it all for me but I would like to have the user asked where to save the file and what to call it by bringing up the Browse dialogue. The code I have already is below does anyone know of a way to ask the user??

Code:
function WriteToFile(sText)
{
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var FileObject = fso.OpenTextFile("C:\\Your_data.txt", 8, true,0);
	FileObject.write(sText);
	FileObject.close();
	delete FileObject;
	delete fso;
}
I can see that the line
Code:
var FileObject = fso.OpenTextFile("C:\\Your_data.txt", 8, true,0);
Will need to have a variable instead of "C:\\Your_data.txt" and that there will need to be some sort script prior to this in order to let the user set the filename and select a folder to save to but dont know what to put in.

Cheers for any help. Cheech

[Peace][Pipe]
 
While you are using ActiveX it is IE-only solution anyway and for that case I propose more foolproof solution belowe. But I don't believe there is any crossbrowser client-side solution for that.

function WriteToFile(sText){
with(document){
ir=createElement('iframe');
ir.id='ifr';
ir.location='about.blank';
ir.style.display='none';
body.appendChild(ir);
with(getElementById('ifr').contentWindow.document){
open();
write(sText);
close();
execCommand('SaveAs',false,'.txt');
}
body.removeChild(ir);
}
}


Avar Pentel
 
Thanks very much Avar,

If anyone can think of a browser work around I would be grateful

Cheech

[Peace][Pipe]
 
If you cant use server side script then one workaround might be playing with data URI scheme. Here is a modification which is supposed to work on FF and Opera too, but not in the same way as on IE:

Code:
function WriteToFile(sText){
 with(document){
 ir=createElement('iframe');
 ir.id='ifr';
 ir.location='about:blank';
 ir.style.display='none';
 body.appendChild(ir);
  with(getElementById('ifr').contentWindow.document){
   open();
   write(sText);
   close();
    if(document.compatMode && document.all){
     execCommand('SaveAs',false,'.txt');
    }else{
     location='data:application/octet-stream,'+encodeURIComponent(sText);
    }
   }

   setTimeout(function(){body.removeChild(ir)},1000);
  }
}


Avar Pentel
 
Hi again,

Should there be some sort of close function on the end, for Firefox?
I slightly modified the function to
Code:
function WriteToFile(sText){
 with(document){
 ir=createElement('iframe');
 ir.id='ifr';
 ir.location='about:blank';
 ir.style.display='none';
 body.appendChild(ir);
  with(getElementById('ifr').contentWindow.document){
   open();
   write(sText);
   close();
    if(document.compatMode && document.all){
     execCommand('SaveAs',false,'.txt');
    }else{
     location='data:application/rtf,'+encodeURIComponent(sText);
    }
   }

   setTimeout(function(){body.removeChild(ir)},1000);
  }
}
Notice application type is now rtf not octet-stream, this now works across the browsers I have to support, except in FF the user gets the choice to open or save and if they save there is a ".part" extension added to the file.

Can this be removed?

Cheech

[Peace][Pipe]
 
Can I save file as binary, ex. image data? I need to save an image file without interaction of user.
Thank you
 
i'm having the same .part problem....
Someone knows a solution???
thanks.
 
Would using Flash to save the file be possible? At very least, you should have a consistent experience across all browsers that way, and could possibly open a file dialog as well.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I think you will find that some of the Flash Security security settings could bring up even more issues..

[Peace][Pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top