C# 在GridView里面使用a标签下载文件(图片)

不能使用ajax进行下载文件的操作,具体原因需百度

前端页面,在GridView里面使用模板列,模板列放a标签

   <cimesui:cimesGridView ID="GridView1" runat="server" AutoGenerateColumns="False"  CssClass="left"
                  DataKeyNames="ID" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted"
                 OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:CommandField HeaderText="操作"  ShowDeleteButton="True" >
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:CommandField>
                    <asp:TemplateField HeaderText="" HeaderStyle-Width="200px">
                                    <ItemTemplate>
                                        <a href="javascript:;" onclick="return GetSelectedRow(this)">下载</a>             
                                    </ItemTemplate>
                                </asp:TemplateField>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
                    <asp:BoundField DataField="filename" HeaderText="文件名" />
                    <asp:BoundField DataField="filesize" DataFormatString=" {0} Byte" HeaderText="文件尺寸">
                        <ItemStyle HorizontalAlign="Right" />
                    </asp:BoundField>
                </Columns>
                <EmptyDataTemplate>
                    没有附档存在!
                </EmptyDataTemplate>
            </cimesui:cimesGridView>  

 

   //前端页面的GetSelectedRow方法
<script type="text/javascript">
//获取选中行的ID,并打开一般处理程序页面
        function GetSelectedRow(UserLink) {
            var row = UserLink.parentNode.parentNode;
            var id = row.cells[2].innerHTML;
            window.location.href = "../Function/IQCGetUploadFile.ashx?id=" + id+"";
        }
    </script>
//一般处理程序(后缀名.ashx) IQCGetUploadFile.ashx  页面代码

 public class IQCGetUploadFile : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try

            {
                //string ID = context.Request["id"].ToString();
                string ID = context.Request.QueryString["id"];
                string filePath = InspectDAC.GetFilePath(ID);
                string fileName = filePath.Substring(filePath.LastIndexOf('\\')).Substring(1);


                byte[] output = InspectDAC.OutputAttachmentFile(ID);
                //以字符流的形式下载文件
                FileStream fs = new FileStream(filePath, FileMode.Open);

                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                HttpContext.Current.Response.Clear();
                fs.Close();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.BinaryWrite(bytes);
                HttpContext.Current.Response.WriteFile(filePath);
                HttpContext.Current.Response.Flush();
                //context.Response.End();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }

            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

posted on 2022-11-03 14:16  写个笔记  阅读(112)  评论(0编辑  收藏  举报

导航