ASP.NET中下载文件的几种实例代码
//transmitfile实现下载
protected void button1_click(object sender, eventargs e)
{
/*
微软为response对象提供了一个新的方法transmitfile来解决使用response.binarywrite
下载超过400mb的文件时导致aspnet_wp.exe进程回收而无法成功下载的问题。
代码如下:
*/
response.contenttype = "application/x-zip-compressed";
response.addheader("content-disposition", "attachment;filename=z.zip");
string filename = server.mappath("download/z.zip");
response.transmitfile(filename);
}
//writefile实现下载
protected void button2_click(object sender, eventargs e)
{
/*
using system.io;
*/
string filename = "asd.txt";//客户端保存的文件名
string filepath = server.mappath("download/aaa.txt");//路径
fileinfo fileinfo = new fileinfo(filepath);
response.clear();
response.clearcontent();
response.clearheaders();
response.addheader("content-disposition", "attachment;filename=" + filename);
response.addheader("content-length", fileinfo.length.tostring());
response.addheader("content-transfer-encoding", "binary");
response.contenttype = "application/octet-stream";
response.contentencoding = system.text.encoding.getencoding("gb2312");
response.writefile(fileinfo.fullname);
response.flush();
response.end();
}
//writefile分块下载
protected void button3_click(object sender, eventargs e)
{
string filename = "aaa.txt";//客户端保存的文件名
string filepath = server.mappath("download/aaa.txt");//路径
system.io.fileinfo fileinfo = new system.io.fileinfo(filepath);
if (fileinfo.exists == true)
{
const long chunksize = 102400;//100k 每次读取文件,只读取100k,这样可以缓解服务器的压力
byte[] buffer = new byte[chunksize];
response.clear();
system.io.filestream istream = system.io.file.openread(filepath);
long datalengthtoread = istream.length;//获取下载的文件总大小
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachment; filename=" + httputility.urlencode(filename));
while (datalengthtoread > 0 && response.isclientconnected)
{
int lengthread = istream.read(buffer, 0, convert.toint32(chunksize));//读取的大小
response.outputstream.write(buffer, 0, lengthread);
response.flush();
datalengthtoread = datalengthtoread - lengthread;
}
response.close();
}
}
//流方式下载
protected void button4_click(object sender, eventargs e)
{
string filename = "aaa.txt";//客户端保存的文件名
string filepath = server.mappath("download/aaa.txt");//路径
//以字符流的形式下载文件
filestream fs = new filestream(filepath, 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(filename, system.text.encoding.utf8));
response.binarywrite(bytes);
response.flush();
response.end();
}
//----------------------------------------------------------
public void downloadfile( system.web.ui.page webform,string filenamewhenuserdownload ,string filebody )
{
webform.response.clearheaders();
webform.response.clear();
webform.response.expires = 0;
webform.response.buffer = true;
webform.response.addheader("accept-language", "zh-tw");
//'文件名称
webform.response.addheader("content-disposition", "attachment; filename='"+system.web.httputility.urlencode(filenamewhenuserdownload, system.text.encoding.utf8)+"'");
webform.response.contenttype = "application/octet-stream";
//'文件内容
webform.response.write(filebody);//-----------
webform.response.end();
}
//上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:
public void downloadfilebyfilepath( system.web.ui.page webform,string filenamewhenuserdownload ,string filepath )
{
webform.response.clearheaders();
webform.response.clear();
webform.response.expires = 0;
webform.response.buffer = true;
webform.response.addheader("accept-language", "zh-tw");
//文件名称
webform.response.addheader("content-disposition", "attachment; filename='" + system.web.httputility.urlencode(filenamewhenuserdownload, system.text.encoding.utf8) +"'" );
webform.response.contenttype = "application/octet-stream";
//文件内容
webform.response.write(system.io.file.readallbytes(filepath));//---------
webform.response.end();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--保存文档
string docname, docextended;
stream doc = fudoc.filecontent;
int doclength = fudoc.postedfile.contentlength;
byte[] docdata = new byte[doclength];
doc.read(docdata, 0, doclength);
docextended = fudoc.filename.substring(fudoc.filename.indexof("."));
if (string.isnullorempty(tbdoc.text))
docname = fudoc.filename;
else
docname = tbdoc.text + docextended;
safetydocinfo data = new safetydocinfo(ddlsort.text, docname, fudoc.postedfile.contenttype, docdata);
safetydoc safety = new safetydoc();
safety.insert(data);
tbdoc.text = string.empty;
--打开文档
public void viewsafetydoc(string pdocsort, string pdocname)
{
oracleparameter[] parms = getsafetydocparm(sql_view_safetydoc);
parms[0].value = pdocsort;
parms[1].value = pdocname;
using (oracledatareader rdr = oraclehelper.executereader(oraclehelper.connectionstring, commandtype.text, sql_view_safetydoc, parms))
{
while (rdr.read())
{
httpcontext.current.response.clear();
httpcontext.current.response.buffer = true;
httpcontext.current.response.charset = "gb2312";
httpcontext.current.response.contentencoding = system.text.encoding.getencoding("gb2312");
httpcontext.current.response.contenttype = rdr.getstring(1);
httpcontext.current.response.binarywrite((byte[])rdr["doc"]);
string filename = rdr.getstring(0);
filename = system.web.httputility.urlencode(filename, system.text.encoding.utf8);
httpcontext.current.response.appendheader("content-disposition", "attachment;filename=" + filename);
}
}
}
上一篇: 作为Java程序员应该掌握的10项技能