提示用户在 ASP.NET C# 中保存/打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10818031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:22:52  来源:igfitidea点击:

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

c#asp.net

提问by Ber53rker

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.

似乎有很多不同的方法(StreamWriter、HttpResponse 等),但是我无法找到任何示例都可以正常工作或解释发生了什么。提前致谢。

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?

第 2 行说要替换该字符串。如何?这段代码被宣传为弹出一个对话框。我不应该在代码中设置路径,对吗?

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

编辑:最终结果(再次编辑,删除必须在 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();

采纳答案by Blachshma

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

第 2 行(FilePath)表示服务器上文件的路径

Line 8:

第 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:

无论如何,我认为第 8 行应该是:

    response.TransmitFile(FilePath + FileName);

回答by Jakub Konecki

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

FilePath应该指向您要发送给客户端的文件。这是服务器上的路径。

回答by Rahul R.

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:

浏览器会出现一个默认对话框,如果它会找到 Response 作为某个文件。如果您希望浏览器显示该默认对话框,您需要做的就是将响应作为文件发送到浏览器,您可以通过多种方式执行此操作:

  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.

  1. 如果是静态文件,

    • 最好的方法是在锚标记的 href 中提及文件路径。(显然,如果您没有安全问题)
    • 就在你的回应中,在你的例子中是这样做的。
    • 您可以在此处参考的其他方法从 asp.net 发送 pdf 的 4 种方法
  2. 如果它是您需要在运行时生成的动态文件,您可以做一个技巧,从文件流生成文件,将其放在服务器的某个临时文件夹中,如上所述将其作为静态文件读回。

回答by Zahid Mir

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