首页 > 编程语言 >C# 在GridView里面使用a标签下载文件(图片)

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

时间:2022-11-03 14:25:22浏览次数:46  
标签:GridView filePath C# 标签 ID Current context HttpContext Response

不能使用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;
            }
        }
    }

 

标签:GridView,filePath,C#,标签,ID,Current,context,HttpContext,Response
From: https://www.cnblogs.com/kelenote/p/16854318.html

相关文章

  • 导航栏下拉列表/vue/scss/html
    效果   scss样式 html 源码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=......
  • implict GEMM
    0x00baseofim2colhttps://zhuanlan.zhihu.com/p/4913073280x01baseofimplictGEMMhttps://zhuanlan.zhihu.com/p/372973726sofar,0x00重点看im2col,0x01重点......
  • javascript规范中美元符号$是什么?
    用途:一般用$获取页面中的某一个对象的id。参考:https://www.cnblogs.com/xutao1517588477/p/10582463.html>>拓展:$(function(){}),什么时候执行?https://blog.csdn.net/u0......
  • WebSocket C#服务器端 当网页刷新时出现无法重连 C#出错:数字小于数组在第一维的下限。
    最近两天公司 要用到 WebSocketC#服务器端+Vue客户端我之前做 WebSocket 是 C#服务器端+原生js客户端原生js客户端 我用iframe 将 WebSocket 用单独一个网......
  • leetcode257-二叉树的所有路径
    257.二叉树的所有路径 泪目,自己写出的递归遍历./***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*......
  • 2022-11-03 Acwing每日一题
    本系列所有题目均为Acwing课的内容,发表博客即是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我......
  • Codeforces Round #610 (Div. 2) C
    C.PetyaandExamhttps://codeforces.com/contest/1282/problem/C考虑贪心先对于时间排序然后贪心我们可以不考那我们可以在此之前离开然后在离开之前这段时间多做......
  • C# HttpClient 封装
    usingSystem.Text;namespaceHTTPClientPacking{publicclassHttpClientHelper{privatestaticHttpClientHelper?_httpClientHelper=null;......
  • Elasticsearch安装部署
    本文只分享windows下安装流程安装版本为:elasticsearch-8.4.3kibana-8.4.3kibana汉化   双击运行 bin文件下的elas......
  • clip-path属性深入理解与使用
      clip-pathCSS属性可以创建一个只有元素的部分区域可以显示的剪切区域。区域内的部分显示,区域外的隐藏。clip-path的属性值可以是以下几种:1.inset; 将元素剪裁为......