最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Jquery Ajax实现文件下载实例代码
时间:2014-04-07 编辑:简简单单 来源:一聚教程网
考虑可以使用jquery ajax提交form请求的方式。
jquery download函数:
代码如下 | 复制代码 |
// 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 += ''; }); // request发送请求 jQuery('') .appendTo('body').submit().remove(); }; }; |
用jquery的方式组织一个字符串,模拟提交一个form请求。
也就是动态渲染表单,提交表单后再删除。
html的图片代码:
代码如下 | 复制代码 |
|
asp.net服务器端代码:aspx文件:
微软为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); // ////??uffer 中的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 流方式下载:
代码如下 | 复制代码 |
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。
其他浏览器无法预计效果。
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31