×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Save file?

Save file?

Save file?

(OP)
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

PeacePipe

RE: Save file?

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

RE: Save file?

(OP)
Thanks very much Avar,

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

Cheech

PeacePipe

RE: Save file?

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

RE: Save file?

(OP)
Many Thanks

PeacePipe

RE: Save file?

(OP)
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

PeacePipe

RE: Save file?

Can I save file as binary, ex. image data? I need to save an image file without interaction of user.
Thank you

RE: Save file?

i'm having the same .part problem....
Someone knows a solution???
thanks.

RE: Save file?


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 @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Save file?

(OP)
I think you will find that some of the Flash Security  security settings could bring up even more issues..

PeacePipe

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close