简体   繁体   English

ASP.NET下载对话框

[英]ASP.NET download dialog box

I have this code.. 我有这个代码。

<li><a href="downloads/PDF_File.pdf">PDF</a></li>

but it opens the pdf file, now I am new to ASP.NET, how do I get the download dialog box to open? 但是它会打开pdf文件,现在我是ASP.NET的新手,如何打开下载对话框?

Basically, what is happening here is the normal behaviour for pdf files. 基本上,这里发生的是pdf文件的正常行为。 IIS by default serves up the "pdf" MIME-type for any pdf files in your web application. 默认情况下,IIS为Web应用程序中的所有pdf文件提供“ pdf” MIME类型。 When you access a pdf in your application, the browser reads the MIME-type and understands that you are accessing a pdf file.. most browsers will want to display it in their built-in PDF reader instead of prompting you to save it. 当您在应用程序中访问pdf时,浏览器将读取MIME类型并理解您正在访问pdf文件。大多数浏览器都希望在其内置的PDF阅读器中显示该文件,而不是提示您保存它。 If you really need a download dialog box to show up for pdfs, you could change the MIME-type for pdf in web.config so that IIS will serve up pdfs as a basic filetype within your application: 如果确实需要一个下载对话框来显示pdf,则可以在web.config中更改pdf的MIME类型,以便IIS在您的应用程序中将pdf用作基本文件类型:

<configuration>
    <system.webServer>
        <staticContent>
            <remove fileExtension=".pdf" />
            <mimeMap fileExtension=".pdf" mimeType="application/octet-stream" />
        </staticContent>
    </system.webServer>
</configuration>

Note: You should first remove the MIME-type you are manually setting in web.config, because a MIME-type for the same extension may already be set at the application level. 注意:您应该首先删除在web.config中手动设置的MIME类型,因为同一扩展名的MIME类型可能已在应用程序级别设置。

Now IIS will serve up pdf files as a basic/unknown filetype and they will be downloadable. 现在,IIS将把pdf文件作为基本/未知文件类型提供,并且可以下载。 This works for any filetype if you just swap out '.pdf' for a different extension. 如果您只是将“ .pdf”换成其他扩展名,则此方法适用于任何文件类型。

I don't know how to do this with existing files, but a while ago I wrote a piece of code combining iTextSharp and an ASP.net (framework 4) MemoryStream object to create then download pdf files, I hope this could be helpful : 我不知道如何使用现有文件执行此操作,但是前一阵子我写了一段代码,将iTextSharp和ASP.net(框架4)MemoryStream对象结合起来创建然后下载pdf文件,希望对您有所帮助:

MemoryStream msPDF = new MemoryStream();
// do some stuff with iTextSharp ...
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=myPdf.pdf"); // open/save dialog
Response.BinaryWrite(msPDF.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM