首页 新闻 会员 周边

文件——下载

0
悬赏园豆:5 [已解决问题] 解决于 2013-10-12 16:56

这是ashx文件代码:

  //从config中读取文件上传路径
                string strFileUploadPath = ConfigurationManager.AppSettings["FileUploadPath"].ToString();
                //从列表框控件中读取选择的文件名
                string strFileName = context.Request.QueryString["filename"];
                //组合成物理路径
                string strFilePhysicalPath = context.Server.MapPath(strFileUploadPath + strFileName);
                //清空输出流
                HttpContext.Current.Response.Clear();
                //在HTTP头中加入文件名信息
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
                //定义输出流MIME类型为
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                //从磁盘读取文件流
                System.IO.FileStream fs = System.IO.File.OpenRead(strFilePhysicalPath);
                //定义缓冲区大小
                byte[] buffer = new byte[102400];
                //第一次读取
                int i = fs.Read(buffer, 0, buffer.Length);
                //如果读取的字节大于0,则使用BinaryWrite()不断向客户端输出文件流
                while (i > 0)
                {
                    HttpContext.Current.Response.BinaryWrite(buffer);
                    i = fs.Read(buffer, 0, buffer.Length);
                }
                //关闭磁盘文件流
                fs.Close();
                //关闭输出流
                //HttpContext.Current.Response.End();
                context.ApplicationInstance.CompleteRequest();

 前台代码:

  

  $(function () {             $("#dgv").datagrid({                 title: "文件列表",                 iconCls: "icon-pt",                 url: "../ajax/upFile.ashx",                 columns: [[                   { field: "id", title: "编号", width: 100 },                   {                       field: "name", title: "名称", width: 200,                       formatter: function (value, rowData, rowIndex) {                           $("#txtName")[0].value = value;                           return '<a id="btnSeen"   onclick="DownLoand()"  class="easyui-linkbutton"  icon="icon-search">' + value + '</a>';                       }                   }                 ]],                 pagination: true,                 rownumbers: true             });         });         function DownLoand() {             //alert($("#txtName")[0].value);

            $.ajax({                 async: false,                 cache: true,                 type: "get",                 url: "download.ashx",                 data: "filename=" + escape($("#txtName")[0].value),                 success: function (result) {                                   }             });         }

在前台success:function(){}如何接收

liyongchao1002的主页 liyongchao1002 | 菜鸟二级 | 园豆:202
提问于:2013-10-10 18:06
< >
分享
最佳答案
0

问题描述不清

收获园豆:5
空明流光 | 初学一级 |园豆:106 | 2013-10-11 08:46

在ashx用来输出文件流,前台如何接收下载 此文件?

liyongchao1002 | 园豆:202 (菜鸟二级) | 2013-10-11 08:53

@liyongchao1002: 这种方式无法下载,你认为javascript的一个函数返回值可以承载一个文件吗?

没有接收这一说,你可以在javascript里,打开一个新页面,这个页面的地址是这个ashx文件的地址,如果需要带上一些get或post参数,它就直接会弹出下载文件话框,如果不想新弹页面,就在当前页面嵌一个iframe,然后将iframe的url设为你的输出文件流的那个页面地址。

说了这么多,不知道你听懂了没有?

空明流光 | 园豆:106 (初学一级) | 2013-10-11 09:45

@沧海一杰: 那这个iframe要嵌入的那里,《iframe的url设为你的输出文件流的那个页面地址 不太理解》?有没有源码可以学习一下。谢谢

liyongchao1002 | 园豆:202 (菜鸟二级) | 2013-10-12 09:10

@liyongchao1002: 好了。谢谢

liyongchao1002 | 园豆:202 (菜鸟二级) | 2013-10-12 10:19
其他回答(2)
0

你是想在前台接收后台执行的结果吗?

我通常会在后台定义两个变量 status  message ,status 记录是否成功,message记录结果信息

然后序列化成json格式到Success:function(result){

  if(result.status)

{

    alert(result.message)

}else

{

  alert(result.message)

}

}

不知道是不是你想要的结果 

Zery | 园豆:6151 (大侠五级) | 2013-10-10 22:02

我想下载输出的文件

支持(0) 反对(0) liyongchao1002 | 园豆:202 (菜鸟二级) | 2013-10-11 08:52
0

你把文件流写入到一个文件里边,然后把路径传到前台,在前台进行下载

翟中龙 | 园豆:97 (初学一级) | 2013-10-11 13:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册