一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net 文件下载代码

时间:2010-08-05 编辑:简简单单 来源:一聚教程网

asp教程.net 文件下载代码

response.clear();
response.buffer = true;
response.contenttype="application/vnd.ms-excel";
response.addheader("content-disposition", "attachment; filename=billinginformation_"+this.ddl_mode.selectedvalue+"("+system.datetime.now.tostring("yyyymmdd")+").csv");
response.contentencoding=system.text.encoding.utf7;
response.charset="";
response.write("export excel file to clients sample");
response.end();


datatable filedt = filedao.getfilebyid(fileid);
byte[] filebytes = (byte[])filedt.rows[0]["filedata"];
string originalfilename = filedt.rows[0]["filename"].tostring();
string randomfilename = guid.newguid().tostring();
//下载到磁盘
string filefullpath = this.server.mappath("~/temp/" + randomfilename);
file.writeallbytes(filefullpath, filebytes);
//发送至客户端
response.clear();
response.clearcontent();
response.clearheaders();
response.contentencoding = system.text.encoding.getencoding("utf-8");
response.addheader("content-disposition", "attachment;filename=" + httputility.urlencode(originalfilename, system.text.encoding.utf8));
response.addheader("content-transfer-encoding", "binary");
response.contenttype = "application/octet-stream";
response.writefile(filefullpath);
response.flush();
//删除磁盘上的文件
if (file.exists(filefullpath))
  file.delete(filefullpath);
response.end();

热门栏目