在不询问saveialog框的情况下阅读pdf

在不询问saveialog框的情况下阅读pdf

本文介绍了在不询问saveialog框的情况下阅读pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,
我想在我们的Web应用程序中读取pdf文件
我使用了generichandler并放置了以下代码.现在用保存对话框打开它.但是我们想不问保存对话框就打开.
请帮忙
谢谢
Soumya

Hai,
I would like to read pdf files in our webapplication
I used generichandler and put the following code.now its opening with save dialog box.but we want to open without asking save Dialog box.
Please help
Thank you,
Soumya

public void ProcessRequest (HttpContext context) {
String sfilepath = context.Server.MapPath("pdf");
string pdffile= sfilepath + "//" + "cat.pdf";
System.IO.FileInfo objFileInfo = new System.IO.FileInfo(exfile);
System.IO.FileStream oStream = null;
String strNewfilename = "Newname_ABC" + objFileInfo.Extension;
if(objFileInfo.Exists)
{
try
{
oStream = objFileInfo.OpenRead();
if (oStream.CanRead)
{
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "inline; filename=\"" + strNewfilename + "\"");
context.Response.WriteFile(pdffile);
oStream.Close();
context.Response.Flush();
context.Response.End();
}
}
}



[已编辑]代码在前置"标记中被阻止[/已编辑]



Code is blocked in "pre" tags[/Edited]

推荐答案

<asp:Literal ID="ltrPdfLink" runat="server"></asp:Literal>



用下面的代码替换您的代码.您需要根据需要修改以下函数中的某些参数,例如 VirtualFilePath .




Replace your code with below one. You need to modify some parameters in below function as per your requirement like VirtualFilePath.


public void ProcessRequest(HttpContext context)
    {
        string exfile = "";
        String sfilepath = context.Server.MapPath("pdf");
        string pdffile = sfilepath + "//" + "cat.pdf";
        System.IO.FileInfo objFileInfo = new System.IO.FileInfo(exfile);
        System.IO.FileStream oStream = null;
        String strNewfilename = "Newname_ABC" + objFileInfo.Extension;
        string VirtualFilePath = "http://yoursite/yourfolder/";
        if (objFileInfo.Exists)
        {
            ltrPdfLink.Text = "<a href=\\" + VirtualFilePath + pdffile + ">Click here for PDF</a>";
        }
    }



您可以将以下行放置在新窗口中打开pdf.



You can place below line to open pdf in new window.

ltrPdfLink.Text = "<a target=''_blank'' href=\\" + VirtualFilePath + pdffile + ">Click here for PDF</a>";



希望对您有所帮助.



Hope it helps.



这篇关于在不询问saveialog框的情况下阅读pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:15