C# 下载C语言的PDF文件#

C# 下载C语言的PDF文件#,c#,asp.net-mvc,pdf,download,C#,Asp.net Mvc,Pdf,Download,在我的应用程序中,我想让用户选择下载PDF文件。在我的代码中,文件通过浏览器打开;但是,我想下载该文件。这是我的密码: 控制器 string name = id; //id is the name of the file string contentType = "application/pdf"; var files = objData.GetFiles(); //list of files string filename

在我的应用程序中,我想让用户选择下载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>

  • 单击“下载”,文件将在浏览器中打开。

    将您的内容类型设置为“应用程序/八位字节流”,这样PDF插件就不会试图拾取并显示它。然后浏览器将其作为文件下载处理。

    从Web下载文件:

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

    下面的代码显示了如何同步下载文件。此方法将阻止主线程,直到文件下载或出现错误(在本例中,引发WebException)。 [C#]:

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


    ref:

    除非您指定不显示文件,否则浏览器将尝试显示该文件

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

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

    浏览器仍试图打开它。我用Chrome和IE试过了。你能试着在返回声明之前添加这一行吗<代码>响应.AddHeader(“内容处置”、“附件;文件名=“+filename”)还是一样。我尝试了Response.AddHeader(“内容处置”、“附件;文件名=“+filename”);和Response.AddHeader(“内容处置”、“附件;文件名=“+FullName”);请尝试引用它,它有点旧,但应该仍然相关。@user3853986 System.Net.Mime.ContentDisposition类具有内联属性请尝试将其设置为true
    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!");
    }
    
    var cd = new System.Net.Mime.ContentDisposition
        {
            FileName = filename, 
            Inline = false, 
        };
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(filename, contentType, FullName);