ajax 实现文件下载 【转】
2015-10-12 19:59
447 查看
<script type="text/javascript">
function DownLoad(strUrl) {
var form = $("<form>"); //定义一个form表单
form.attr('style', 'display:none'); //在form表单中添加查询参数
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', "/QuestionInfo/DowmLoad");
var input1 = $('<input>');
input1.attr('type', 'hidden');
input1.attr('name', 'strUrl');
input1.attr('value', strUrl);
$('body').append(form); //将表单放置在web中
form.append(input1); //将查询参数控件提交到表单上
form.submit();
}
</script>
后台代码
#region 文档下载
/// <summary>
/// 文件下载函数
/// </summary>
/// <param name="fileUrl"></param>
/// <returns></returns>
[HttpPost]
public void DowmLoad(string strUrl)
{
try
{
string fullPathUrl = Server.MapPath(strUrl);//获取下载文件的路劲
System.IO.FileInfo file = new System.IO.FileInfo(fullPathUrl);
if (file.Exists)//判断文件是否存在
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.AddHeader("content-disposition", "attachment;filename=" + file.Name);
Response.AddHeader("cintent_length", "attachment;filename=" + HttpUtility.UrlDecode(file.Name));
Response.AddHeader("cintent_length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);//通过response对象,执行下载操作
Response.Flush();
Response.End();
}
}
catch(Exception e)
{
Console.Write(e.ToString());
}
}
function DownLoad(strUrl) {
var form = $("<form>"); //定义一个form表单
form.attr('style', 'display:none'); //在form表单中添加查询参数
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', "/QuestionInfo/DowmLoad");
var input1 = $('<input>');
input1.attr('type', 'hidden');
input1.attr('name', 'strUrl');
input1.attr('value', strUrl);
$('body').append(form); //将表单放置在web中
form.append(input1); //将查询参数控件提交到表单上
form.submit();
}
</script>
后台代码
#region 文档下载
/// <summary>
/// 文件下载函数
/// </summary>
/// <param name="fileUrl"></param>
/// <returns></returns>
[HttpPost]
public void DowmLoad(string strUrl)
{
try
{
string fullPathUrl = Server.MapPath(strUrl);//获取下载文件的路劲
System.IO.FileInfo file = new System.IO.FileInfo(fullPathUrl);
if (file.Exists)//判断文件是否存在
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.AddHeader("content-disposition", "attachment;filename=" + file.Name);
Response.AddHeader("cintent_length", "attachment;filename=" + HttpUtility.UrlDecode(file.Name));
Response.AddHeader("cintent_length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);//通过response对象,执行下载操作
Response.Flush();
Response.End();
}
}
catch(Exception e)
{
Console.Write(e.ToString());
}
}
相关文章推荐
- 等价类_测试基础
- 让程序在崩溃时体面的退出之Unhandled Exception
- 只有程序员看的懂面试圣经|如何拿下编程面试
- 【Emgu】一起学EmguCV(一)配置与使用
- JavaScript开发工具WebStorm教程:用户界面简介
- HTTP、Scoket网络协议浅解
- 从12大技巧、30个案例、99个模板谈怎么写标题
- POJ 2109 Power of Cryptography 贪心
- asp jquery ajxs json和jsonp跨域请求
- cocos2dx 3.8 安装环境配置搭建
- mysql相关包括事件、事务、以及服务器上自动备份
- jquery判断checkbox是否选中及改变checkbox状态
- android 流量统计
- 让程序在崩溃时体面的退出之终极解决方案(SEH+Dump+Unhandled Exception Filter)
- spark streaming源码分析2 从简单例子看DStream上的operation
- 重新报道csdn
- 【UIPickerView自定义选择器控件】
- WebStorm 通过git取远端项目 cannot run program "git.exe" and Failed
- WebStorm 通过git取远端项目 cannot run program "git.exe" and Failed
- C#连接操作mysql