-1

I'm trying to make a request in javascript to call an aspx function with the intention of it returning a .txt file as a save-as dialog in IE (only needs IE support).

The hitch is I need the Javascript to send a string to the aspx, which then uses the string to dynamically generate a .txt file to send back for saving.

Something like the dynamic example here but with a txt file: http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET

What request do I need to make to the server to get it back as a "Save As" prompt?

Examples would be fantastic.

DIXONJWDD
  • 1,156
  • 9
  • 20

1 Answers1

0

Ok, got it! So here we go for anyone interested:

Asp.net server (save.aspx file):

        string txtString = Request["txtString"];

        Response.Clear();

        Response.AppendHeader(
        "Content-Disposition", 
        "attachment; 
        filename=myFile.txt"
        );

        Response.ContentType = "application/x-download";

        Response.Write(txtString);


        Response.End();

And thanks to this post!!: JavaScript post request like a form submit

Javascript:

function saveTxt() {

    var myString = getTxtString();

    var form = document.createElement("form");
    form.setAttribute("method", "POST");
    form.setAttribute("action", "save.aspx");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "jsonString");
    hiddenField.setAttribute("value", myString);

    form.appendChild(hiddenField);

    document.body.appendChild(form);
    form.submit();
}
Community
  • 1
  • 1
DIXONJWDD
  • 1,156
  • 9
  • 20