2016-11-25 83 views
-1

在我的webform(ViewRequest.aspx)上,我放置了一個gridview用戶可以從網格下載文件。使用webForm名稱以.aspx擴展名的文件下載

文件與刪除和下載填充網格精細button.but當用戶下載從谷歌瀏覽器文件,文件名總是ViewRequest.aspx這是網頁名稱。然後用戶必須通過機制打開以合適的格式打開選定的文件,例如用於PDF文件的PDF閱讀器等。

這個問題在Internet Explorer中不存在。文件名與Grid中的文件名相同,擴展名也是可以的。

我不知道可能是什麼問題。

任何人都可以幫助我。

這裏是我的代碼來下載文件

protected void Grid_Attachments_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "download") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     string url = Grid_Attachments.Rows[index].Cells[3].Text; 
     System.IO.FileInfo file = new System.IO.FileInfo(url); 

     if (file.Exists) 
     { 
      Response.Clear(); 
      Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name); 
      Response.AppendHeader("Content-Length", file.Length.ToString()); 
      Response.ContentType = "application/octet-stream"; 
      Response.TransmitFile(file.FullName); 
      Response.End(); 
     } 
    } 
} 

這是我的aspx頁面代碼:

<asp:GridView ID="Grid_Attachments" runat="server" Width="100%" AutoGenerateColumns="False" EnableModelValidation="True" OnRowDataBound="Grid_Attachments_RowDataBound" OnRowDeleting="Grid_Attachments_RowDeleting" OnRowCommand="Grid_Attachments_RowCommand"> 
          <Columns> 
           <asp:TemplateField HeaderText="Files Added"> 
            <ItemTemplate> 
             <asp:Label ID="LBL_Attachment" runat="server" Text='<%# Bind("FILE_NAME") %>'></asp:Label> 
            </ItemTemplate> 
            <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" /> 
            <ItemStyle CssClass="UserTableRow" /> 
           </asp:TemplateField> 
            <asp:BoundField HeaderText="Added By" DataField="empname"> 
            <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="25%" /> 
            <ItemStyle CssClass="UserTableRow" /> 
           </asp:BoundField> 
            <asp:BoundField HeaderText="Attachment Date" DataField="ATTACHMENT_DATE"> 
            <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="20%" /> 
            <ItemStyle CssClass="UserTableRow" /> 
           </asp:BoundField> 
           <asp:BoundField HeaderText="File Path" DataField="ATTACHMENT_PATH"> 
            <HeaderStyle CssClass="hiddencol" /> 
            <ItemStyle CssClass="hiddencol" /> 
           </asp:BoundField> 
           <asp:ButtonField ControlStyle-BackColor="#C6304A" ControlStyle-ForeColor="White" ButtonType="Button" 
            CommandName="download" HeaderText="Download File(s)" ShowHeader="True" Text="Download"> 
            <ItemStyle ForeColor="#CC0000" HorizontalAlign="Center" VerticalAlign="Middle" /> 
            <ControlStyle BackColor="#C6304A" ForeColor="White"></ControlStyle> 
            <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="15%" /> 
           </asp:ButtonField> 
           <asp:CommandField ShowDeleteButton="True" HeaderText="Action"> 
            <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" /> 
            <ItemStyle ForeColor="#CC0000" HorizontalAlign="Center" VerticalAlign="Middle" /> 
           </asp:CommandField> 
           <asp:BoundField HeaderText="EmpCode" DataField="emp_code"> 
            <HeaderStyle CssClass="hiddencol" /> 
            <ItemStyle CssClass="hiddencol" /> 
           </asp:BoundField> 
          </Columns> 
          <HeaderStyle BackColor="#C6304A" ForeColor="White" /> 
         </asp:GridView> 

這是網綁定代碼:

private void BindAttachmentGrid(string RequestNo) 
    {   
     Grid_Attachments.DataSource = attachments.getAllAttachmentForARequest(RequestNo); 
     Grid_Attachments.DataBind(); 
    } 

更新:

我試過這個代碼,但它只是工作的PDF文件

Response.Clear(); 
       Response.ClearHeaders(); 
       Response.ClearContent(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\""); 
       Response.AddHeader("Content-Length", file.Length.ToString()); 
       string filetype = file.Extension; 
       Response.ContentType = "application/"+filetype; 
       Response.Flush(); 
       Response.TransmitFile(file.FullName); 
       Response.End(); 

回答

0

我想建議您訪問此鏈接:

https://bugs.chromium.org/p/chromium/issues/detail?id=103618 http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

嘗試使用此代碼: :

Response.Clear(); 
Response.ClearHeaders(); 
Response.ClearContent(); 
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\""); 
Response.AddHeader("Content-Length", file.Length.ToString()); 
Response.ContentType = your content type 
Response.Flush(); 
Response.TransmitFile(file.FullName); 
Response.End(); 
+0

沒有什麼好看的,回傳發生 –

+0

can yo你還需要粘貼你的設計頁面代碼嗎?你使用過更新面板嗎? –

+0

好吧我添加的代碼,沒有我沒有使用更新面板 –