0

I am creating a webpage with javascript that is doing some processing and building a data file in a variable. At the end of the processing, I want to provide a link that will let the user right click a link and do save as or something similar to save the results of that variable. Is there a way to do this without posting the results back to the server and writing out the file to the server and then redirecting to another page with the link. Basically I want to be able to provide the user with a way to do a save as or click a link to open the contents of a variable in javascript. I am thinking maybe an iframe and put the contents there?

user999684
  • 655
  • 1
  • 9
  • 20

2 Answers2

1

You might be able to use a data:// URL for that, at least on some browsers.

Alternately, yeah, have the link open a new window and write the contents there. They can then "save as" the new page (live example):

function whenLinkClicked(event) {
    var wnd = window.open();
    wnd.document.write(/* ...contents here... */);
    event = event || window.event;
    if (event.preventDefault) {
        event.preventDefault();
    }
    return false;
}
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
1

JavaScript doesn't have a great support for files until you get into up and coming file API. You could use a JavaScript and flash solution like Downloadify found here:

https://github.com/dcneiner/Downloadify

Declan Cook
  • 6,009
  • 2
  • 34
  • 51