15

It shouldn't be this hard to find out how to do this. Basically I'm trying to take a string and let the client save it when they click a button. It should pop up with a Save/Open dialog. No extra bells and whistles or anything. It's not rocket science, (or so I would've thought).

There seems to be a ton of different ways, (StreamWriter, HttpResponse, etc.), but none of the examples I've been able to find work properly or explain what's going on. Thanks in advance.

An example one of the many blocks of code I've found...

(This is just an example, feel free to not base your answer around this.)

String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();

Line 2 says to replace that string. How? This code was advertised as bringing up a dialog. I shouldn't be having to set a path in the code, right?

EDIT: Final Outcome (Edited again, Delete has to come before End();)

        string FilePath = Server.MapPath("~/Temp/");
        string FileName = "test.txt";

        // Creates the file on server
        File.WriteAllText(FilePath + FileName, "hello");

        // Prompts user to save file
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/plain";
        response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
        response.TransmitFile(FilePath + FileName);
        response.Flush();

        // Deletes the file on server
        File.Delete(FilePath + FileName);

        response.End();
2
  • 1
    This sample shows how to stream a file back to the client. Are you looking for a way to invoke a save/open dialog on the client so that you can upload a file to your server? Commented May 30, 2012 at 14:13
  • 1
    "Basically I'm trying to take a string and let the client save it when they click a button." Something simple like... File.WriteAllText("C:\\text.txt", "hello"); Except with a dialog. I want the user to download it to whatever location they specify. Commented May 30, 2012 at 14:17

4 Answers 4

6

Line 2 (FilePath) indicates the path to the file on the server

Line 8:

response.TransmitFile(FilePath);

Transmits that specific file to the client and THAT is what pops the save dialog.

If you don't transmit the file, I'm not sure if the dialog will pop up at all (even though you set a header)

Anyways, I think line 8 should read:

    response.TransmitFile(FilePath + FileName);
Sign up to request clarification or add additional context in comments.

Comments

4

There will be a default dialog box by browser, if it will find Response as some file. If you want browser to display that default dialog box, all you need to do is send response to browser as file, which you can do in number of ways:

  1. If it is a static file,

    • best way is to just mention path of file in anchor tag's href.(obviously if you don't have security concern)
    • Just out along with your response, the way it is done in your example.
    • Other ways you can refer here 4 ways to send pdf from asp.net
  2. If it is a dynamic file which you need to generate at run time, you can do a trick, generate the file from filestream, put it in some temporary folder at server, read it back as a static file as mentioned above.

Comments

3

Just use this code it should work to prompt the user to open a dialog for opening or saving the file on the system ....

byte[] bytesPDF = System.IO.File.ReadAllBytes(@"C:\sample.pdf");

        if (bytesPDF != null)
        {

            Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.pdf");
            Response.ContentType = "application/octectstream";
            Response.BinaryWrite(bytesPDF);
            Response.End();
        }

1 Comment

BinaryWrite I am doing same thing for excel and data going till client(checked in dev tools) but its not prompting a file save.
2

FilePath is supposed to point to the file you want to send to the client. This is the path on the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.