Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt user to save/open file in ASP.NET C#

Tags:

c#

asp.net

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();
like image 491
Ber53rker Avatar asked May 30 '12 14:05

Ber53rker


People also ask

How to save a file using save file dialog in c#?

To save a file using the SaveFileDialog component. Display the Save File dialog box and call a method to save the file selected by the user. Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.


3 Answers

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);
like image 91
Blachshma Avatar answered Oct 19 '22 20:10

Blachshma


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.

like image 3
Rahul R. Avatar answered Oct 19 '22 19:10

Rahul R.


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

like image 1
Jakub Konecki Avatar answered Oct 19 '22 21:10

Jakub Konecki