-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();
没有什么好看的,回传发生 –
can yo你还需要粘贴你的设计页面代码吗?你使用过更新面板吗? –
好吧我添加的代码,没有我没有使用更新面板 –