简体   繁体   English

在asp.net webform中使用“另存为”选项打开PDF文件

[英]Open PDF file with Save As option in asp.net webform

I have list of PDF files on website (asp.net webforms). 我在网站上有PDF文件列表(asp.net webforms)。 i want to open them with Save As option rather than it downlaods directly. 我想用“另存为”选项打开它们而不是直接下载。

I tried to add download property to the link which didn't work. 我试图将download属性添加到无效的链接。 only was around seems to be HTTPHandler for *.pdf request. 只有周围似乎是*.pdf请求的HTTPHandler。

I saw a piece of code for MVC based example here 在这里看到了一段基于MVC的代码

return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};

How can i convert this to HTTPHandler in as.net webform so that it open pdf files with Save As option. 如何在as.net webform中将其转换为HTTPHandler,以便它使用“另存为”选项打开pdf文件。

I want to do it in a way so that when ever user click on any pdf file at that time Handler should come into action. 我想以某种方式做到这一点,以便当用户点击任何pdf文件时,Handler应该开始行动。

OR 要么

I can create another file handlePDF.aspx and write code there also and will change link of pdf file to below 我可以创建另一个文件handlePDF.aspx并在那里编写代码,并将pdf文件的链接更改为以下

<a href="handlePDF.aspx?file=file1.pdf">File One </a>

If what you are trying to do is when they click on the file download link it pops up with save as or open dialog box, this is to do with the user's browser configuration. 如果你想要做的是当他们点击文件下载链接时会弹出save asopen对话框,这与用户的浏览器配置有关。 In the case of PDF's i believe Firefox has open in tab as the default option. 在PDF的情况下,我相信Firefox已open in tabopen in tab作为默认选项。 If you try to push the file as a file stream it will more than likely just load it in a new tab as well. 如果您尝试将文件作为文件流推送,则很可能只是将其加载到新选项卡中。

tl;dr: Client side issue tl; dr:客户端问题

You're on the right track. 你走在正确的轨道上。 Serving PDF files are usually handled by an HttpHandler . 服务PDF文件通常由HttpHandler处理。 That is, unless they can be served straight from the file system by the StaticHandler ... 也就是说,除非它们可以通过StaticHandler直接从文件系统StaticHandler ...

The key thing that is needed in order for the browser to raise the "Open or save" dialog is the Content-Disposition header in the response. 浏览器引发“打开或保存”对话框所需的关键是响应中的Content-Disposition标头。

Here is an (untested) implementation that should get you on the right track: 这是一个(未经测试的)实现,可以让您走上正确的轨道:

public void ProcessRequest(HttpContext context)
{
    string fileName = context.Request.QueryString["file"];
    if(fileName == null || fileName == "")
    {
        throw new ArgumentException("The file argument cannot be null or empty");
    }

    // fetch file here from db/filesystem/other storage
    byte[] fileBytes = LoadFileBytes(fileName);

    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    context.Response.ContentType = "application/pdf";
    context.Response.BinaryWrite(fileBytes);
}

If you want to avoid buffering the whole file in memory, this might also work (requires .Net 4.0 for the CopyTo method on the stream): 如果你想避免在内存中缓冲整个文件,这可能也有效(在流上的CopyTo方法需要.Net 4.0):

public void ProcessRequest(HttpContext context)
{
    string fileName = context.Request.QueryString["file"];
    if(fileName == null || fileName == "")
    {
        throw new ArgumentException("The file argument cannot be null or empty");
    }

    // fetch file stream from db/filesystem/other storage
    Stream fileStream = GetFileStream(fileName);

    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    context.Response.ContentType = "application/pdf";
    fileStream.CopyTo(context.Response.OutputStream);
}

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

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