C# 保存对话框下载文件,将文件从 ASP.NET 服务器保存到客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12833490/
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-10 00:36:24  来源:igfitidea点击:

Save dialog box to download file, Saving file from ASP.NET server to the client

c#asp.netsavesave-dialog

提问by user1734609

I've been searching around the internet, but couldn't find any useful answer.

我一直在互联网上搜索,但找不到任何有用的答案。

I have an ASP.NET web site, which is deployed on server. The ASP.NET web site on the server can access a directory called W:/ . The clients in the company can access the web site. The web site lists in a ListBox all the PDF files from the W:/ directory. The client should be able to select PDF files from the listbox and save them to it's local PC by selecting a location for it.

我有一个 ASP.NET 网站,它部署在服务器上。服务器上的 ASP.NET 网站可以访问名为 W:/ 的目录。公司内的客户可以访问该网站。该网站在一个列表框中列出了 W:/ 目录中的所有 PDF 文件。客户端应该能够从列表框中选择 PDF 文件,并通过为其选择位置将它们保存到本地 PC。

Something like save as file on web pages.

类似于在网页上另存为文件。

Could you provide me some solution or work around ?

你能给我提供一些解决方案或解决办法吗?

回答by Aristos

The correct keywords are "File Browser asp.net" to find a lot of examples with source code.

正确的关键字是“File Browser asp.net”,可以找到很多带有源代码的示例。

Here is one from codeproject:

这是 codeproject 中的一个:

http://www.codeproject.com/Articles/301328/ASP-NETUser-Control-File-Browser

http://www.codeproject.com/Articles/301328/ASP-NETUser-Control-File-Browser

回答by Aristos

Get file contents in byte[] from W drive and write it to local file.

从 W 驱动器获取 byte[] 中的文件内容并将其写入本地文件。

byte[] data = File.ReadAllBytes(WDriveFilePath)

FileStream file = File.Create(HttpContext.Current.Server.MapPath(MyLocalFile)); 

file.Write(data, 0, data.Length); 
 file.Close(); 

回答by user1734609

Finally I've found an article, which Prompts a Save Dialog Box to Download a File from ASP.NET

最后我找到了一篇文章,它提示保存对话框从 ASP.NET 下载文件

I post it here, might help somebody else as well and save some time.

我把它贴在这里,也可以帮助其他人并节省一些时间。

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

回答by Jithesh Chandra

I have done something like this to get the file .

我已经做了这样的事情来获取文件。

protected void btnExportFile_Click(object sender, EventArgs e)
        {
            try
            {
                Thread newThread = new Thread(new ThreadStart(ThreadMethod));
                newThread.SetApartmentState(ApartmentState.STA);
                newThread.Start();     

               // try using threads as you will get a Current thread must be set to single thread apartment (STA) mode before OLE Exception .


            }
            catch (Exception ex)
            {

            }

        }

        static void ThreadMethod()
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
        }

回答by Mehdi Benkirane

This is an extension to user1734609's solution that gets a file locally.

这是 user1734609 在本地获取文件的解决方案的扩展。

To download a file from the server to client:

要将文件从服务器下载到客户端:

public void DownloadFile()
        {
            String FileName = "201604112318571964-sample2.txt";
            String FilePath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/Uploads/" + FileName;
            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();


        }