【问题标题】:Save dialog box to download file, Saving file from ASP.NET server to the client保存对话框以下载文件,将文件从 ASP.NET 服务器保存到客户端
【发布时间】:2012-10-11 06:29:52
【问题描述】:

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

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

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

您能否提供一些解决方案或解决方法?

【问题讨论】:

    标签: c# asp.net save save-dialog


    【解决方案1】:

    终于找到一篇文章,提示保存对话框从 ASP.NET 下载文件

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

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

    【讨论】:

    • 感谢 Cory Mathews 的文章。
    • 它可以用于下载 .zip 文件吗?应该使用什么 ContentType?
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      正确的关键字是“文件浏览器asp.net”找到很多带有源代码的示例。

      这是来自codeproject的一个:

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

      【讨论】:

      • @user1734609 这是你要找的吗? :)
      • 我已经通读了这篇文章,但不完全是:) 我在 ListBox 中有 PDF 文件名。 W:/ 目录中的文件位于不同的服务器上,但在域内。列表框列出了该目录的所有文件名。公司内部的客户端在打开网站时可以从目录中获取PDF文件列表。然后选择一个或多个并单击另存为。并且应该可以保存在自己的PC上:) 你明白重点吗? :)
      • @user1734609 是的,我明白了。您从该代码开始,然后在选择文件以将其显示给浏览器时,从包含所有域的表中进行选择。我不知道你如何设置它,也许是共享目录,也许是 ftp 读取,也许是克隆......
      【解决方案4】:

      从W盘获取byte[]中的文件内容并写入本地文件。

      byte[] data = File.ReadAllBytes(WDriveFilePath)
      
      FileStream file = File.Create(HttpContext.Current.Server.MapPath(MyLocalFile)); 
      
      file.Write(data, 0, data.Length); 
       file.Close(); 
      

      【讨论】:

      • 这不提供保存对话框。
      【解决方案5】:

      我做了类似的事情来获取文件。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 2021-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多