2

我有一个gridview,它包含文件名和文件路径(图像和pdf格式文件),因为我使用了模板字段,在该字段下放置了1个图像按钮。单击该图像按钮即查看按钮时,我想在新窗口中打开选定的文件。

这是我的代码:

protected void GVViewFile_SelectedIndexChanged(object sender, EventArgs e)
{
    int id = GVViewFile.SelectedIndex;
    string path = GVViewFile.Rows[id].Cells[2].Text.ToString();

    Response.Redirect("D:\UploadedAttachment\AT\MRD\AT0520130008_15-05-13-03-57-12.pdf");

    Response.Write("<script>");
   Response.Write("window.open('" + path + "','_blank', ' fullscreen=yes')");
   //Response.Write("window.open(" + path + ",'_blank')");
   Response.Write("</script>");

}

但我无法在新窗口中打开。我的路径返回与内部 response.write() 相同的值。当我使用只是response.write("images/UserDetails.pdf");作为示例时,它将显示 pdf 页面..但未采用完整路径。它还显示'\'错误,response.write();因此如何使用实际完整路径在新窗口中显示图像或pdf..请帮助我。即使window.open给出错误。我无法在window.open中写入完整路径,因为我被选中了来自gridview.help的路径请......

我的gridview代码:

 <asp:GridView ID="GVViewFile" runat="server" AutoGenerateColumns="False" 
        DataSourceID="DSforgridview" onselectedindexchanged="GVViewFile_SelectedIndexChanged"
        HeaderStyle-BackColor="#CC6600" HeaderStyle-ForeColor="White" 
    PagerStyle-BackColor="#CC6600" PagerStyle-ForeColor="White" CellPadding="3" 
    CellSpacing="3" PagerStyle-Width="4" PagerStyle-Height="4" 
    BorderColor="#FF6600" BorderStyle="Solid">
        <Columns>
            <asp:TemplateField ShowHeader="false">
        <ItemTemplate>
            <asp:ImageButton ID="btnView" runat="server" 
                CausesValidation="False" CommandName="Select"
                ImageUrl="~/Images/view.gif" ToolTip="View File" />
        </ItemTemplate>
      </asp:TemplateField>

            <asp:BoundField DataField="FileType" HeaderText="FileType" 
                SortExpression="FileType" />
            <asp:BoundField DataField="FileLocationPath" HeaderText="FileLocationPath" 
                SortExpression="FileLocationPath" />
        </Columns>
    <HeaderStyle BackColor="#CC6600" ForeColor="White"></HeaderStyle>
    <EmptyDataTemplate>No Records Found.</EmptyDataTemplate>
    </asp:GridView>
4

6 回答 6

5
//In Default2.aspx
protected void LinkButton1_Click(object sender, EventArgs e)
    {
       Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Default3.aspx"));
    }

//------------
//In Default3.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        string path = Server.MapPath("~\\E:\\karthikeyan\\venky\\pdf\\aaaa.PDF");
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(path);
        if (buffer != null)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
        }
    }
于 2013-05-16T07:57:56.033 回答
0

It will work with relative path only. Why in the first place you need path? Also user Registerstartupscript for script binding to page.

于 2013-05-16T05:23:21.123 回答
0

In html response you are operating with url paths. So path to open should be valid url (absolute or relative to application), or link to file: "file://path/to/file", which open some directory browser in computer.

You can just put HyperLink control with NavigateUrl with target="_blank" or some javascript. Link to absolute server path will not work.

于 2013-05-16T05:23:42.533 回答
0

当你绑定你的 FileLocationPath 时,尝试绑定它,以便你的文件名

D:\UploadedAttachment\AT\MRD\AT0520130008_15-05-13-03-57-12.pdf

变得

file:///D:/UploadedAttachment/AT/MRD/AT0520130008_15-05-13-03-57-12.pdf
于 2013-05-16T06:40:07.247 回答
0
 Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "pdf/aaaa.PDF"));
于 2013-05-16T06:34:07.073 回答
0

abort() 函数可能是您最好的选择。它是 C 标准库的一部分,被定义为“导致程序异常终止”(例如,致命错误或崩溃)。

于 2017-05-27T03:37:19.073 回答