zoukankan      html  css  js  c++  java
  • Jquery+asp.net实现Ajax方式文件下载实例代码

    如果文件中,需要下载一个文件,一般用超级链接的方式即可。

    但是如果是图片,浏览器会默认打开图片浏览,不是实现下载。

    考虑可以使用jquery ajax提交form请求的方式。

    jquery download函数:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     // Ajax 文件下载
        jQuery.download = function (url, data, method) {
            // 获取url和data
            if (url && data) {
                // data 是 string 或者 array/object
                data = typeof data == 'string' ? data : jQuery.param(data);
                // 把参数组装成 form的  input
                var inputs = '';
                jQuery.each(data.split('&'), function () {
                    var pair = this.split('=');
                    inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
                });
                // request发送请求
                jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>')
            .appendTo('body').submit().remove();
            };
        };

    用jquery的方式组织一个字符串,模拟提交一个form请求。

    也就是动态渲染表单,提交表单后再删除。

    html的图片代码:

    1
    <img onclick="GetSrcFromSvc('" + name + "')" src="" + imgurl + "" //>

    GetSrcFromSvc函数实现调用:

    1
     $.download("http://localhost:2204/wx/Default.aspx""img=" + url, 'post');

    asp.net服务器端代码:aspx文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite下载超过
     400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。  ///指定被输出图像的地址
      
            string imgurl = Request.Form["img"];
            string FileName = Server.MapPath(imgurl);
            //   System.Drawing.Image img = System.Drawing.Image.FromFile(imgurl);
            //   MemoryStream ms = new System.IO.MemoryStream();
            //   img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            //   img.Dispose();
            //   context.Response.ClearContent();
            //   context.Response.ContentType = "image/jpg";
            //   context.Response.BinaryWrite(ms.ToArray());
            //   //context.htm = htm&File(FileName);
            //   ////將buffer 中的stream全部送出
            //    context.Response.Flush();
            ////   context.Response.End();
            string filePath = Server.MapPath(imgurl);//路径 
            if (File.Exists(filePath))
            {
                FileInfo fileinfoo = new FileInfo(filePath);
                Response.ContentType = "application/x-zip-compressed";
                Response.AddHeader("Content-Disposition""attachment;filename=" + fileinfoo.Name + "");
                Response.TransmitFile(filePath);
            }
            else
            {
                htm = htm&("未找到文件。");
            }

    asp.net 流方式下载:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
         string imgurl = Request.Form["img"];
            string FileName = Server.MapPath(imgurl);
            if (File.Exists(FileName))
            {
                FileInfo fileinfoo = new FileInfo(FileName);
                //以字符流的形式下载文件
                FileStream fs = new FileStream(FileName, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开
                Response.AddHeader("Content-Disposition""attachment;   filename=" + HttpUtility.UrlEncode(fileinfoo.Name, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }

    测试环境

    win7+IE9 IE10 。手机端:uc。

    其他浏览器无法预计效果。

  • 相关阅读:
    css字体属性相关。
    子级用css float浮动 而父级div没高度不能自适应高度
    转载:基于Redis实现分布式锁
    LeetCode(53):最大子序和
    LeetCode(52):N皇后 II
    LeetCode(51):N皇后
    LeetCode(50):Pow(x, n)
    LeetCode(49): 字母异位词分组
    LeetCode(48):旋转图像
    LeetCode(47):全排列 II
  • 原文地址:https://www.cnblogs.com/telwanggs/p/4865694.html
Copyright © 2011-2022 走看看