【问题标题】:Download PDF File in C#用 C# 下载 PDF 文件
【发布时间】:2014-09-30 21:59:54
【问题描述】:

在我的应用程序中,我想为用户提供下载 PDF 文件的选项。在我的代码中,文件由浏览器打开;但是,我希望下载文件。这是我的代码:

控制器

        string name = id; //id is the name of the file
        string contentType = "application/pdf";

        var files = objData.GetFiles(); //list of files


        string filename = (from f in files
                           orderby f.DateEncrypted descending
                           where f.FileName == name
                           select f.FilePath).First(); //gets the location of the file

        string FullName = (from f in files
                           where f.FileName == name
                           select f.FileName).First(); //gets the new id in new location to save the file with that name



        //Parameters to File are
        //1. The File Path on the File Server
        //2. The content type MIME type
        //3. The parameter for the file save by the browser
        return File(filename, contentType, FullName);

这是我在下拉菜单中使用它的方式。

查看

<li><a id="copyURL" href="@Url.Action("Download", "Home", new { id = item.FileName})">Download</a></li>

通过点击“下载”,文件被浏览器打开。

【问题讨论】:

    标签: c# asp.net-mvc pdf download


    【解决方案1】:

    将您的内容类型设置为“application/octet-stream”,这样 PDF 插件就不会尝试提取并显示它。然后浏览器会将其作为文件下载处理。

    【讨论】:

    • 浏览器仍然尝试打开它。我用 Chrome 和 IE 都试过了。
    • 您可以尝试在您的退货声明之前添加此行吗? Response.AddHeader("content-disposition", "attachment; filename="+filename);
    • 还是一样。我试过 Response.AddHeader("content-disposition", "attachment; filename="+filename);和 Response.AddHeader("content-disposition", "attachment; filename="+FullName);
    • 尝试引用这个question,它有点旧,但应该仍然相关。
    • @user3853986 System.Net.Mime.ContentDisposition 类具有 Inline 属性尝试将其设置为 true
    【解决方案2】:

    从网上下载文件:

    这个例子展示了如何从任何网站下载文件到本地磁盘。下载文件的简单方法是使用 WebClient 类及其方法 DownloadFile。该方法有两个参数,第一个是要下载的文件的 url,第二个参数是要保存文件的本地磁盘的路径。 同步下载文件

    以下代码显示了如何同步下载文件。此方法会阻塞主线程,直到文件下载或发生错误(在这种情况下会引发 WebException)。 [C#]:

    using System.Net;
    
    WebClient webClient = new WebClient();
    webClient.DownloadFile("pdf file address", @"c:\myfile.pdf");
    

    异步下载文件: 要在不阻塞主线程的情况下下载文件,请使用异步方法 DownloadFileAsync。您还可以设置事件处理程序以显示进度并检测文件是否已下载。 [C#]:

    private void btnDownload_Click(object sender, EventArgs e)
    {
      WebClient webClient = new WebClient();
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
      webClient.DownloadFileAsync(new Uri("pdf file address"), @"c:\myfile.pdf");
    }
    
    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
      progressBar.Value = e.ProgressPercentage;
    }
    
    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
      MessageBox.Show("Download completed!");
    }
    

    参考:http://www.csharp-examples.net/download-files/

    【讨论】:

      【解决方案3】:

      浏览器将尝试显示文件,除非您指定不这样做。

      在返回文件之前尝试添加 ContentDisposition。

      var cd = new System.Net.Mime.ContentDisposition
          {
              FileName = filename, 
              Inline = false, 
          };
      Response.AppendHeader("Content-Disposition", cd.ToString());
      return File(filename, contentType, FullName);
      

      【讨论】:

        猜你喜欢
        • 2012-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        • 1970-01-01
        • 2016-09-12
        相关资源
        最近更新 更多