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

2020-06-10 11:13发布

问题:

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

回答1:

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);


回答2:

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.



回答3:

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



回答4:

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();
        }


标签: c# asp.net