asp教程.net xls 文件下载功能实例
提供二款asp.net教程 c文件下载实例代码,如果你直接连接xls文件,会在浏览器打开,现在我们利用.net来实例点击下载。
*/
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback)
{
string filepath = "d:www.111cn.net.xls";
// filepath = "d:test.xls";
system.io.filestream fs = system.io.file.openread( filepath );
byte[] filedata = new byte[fs.length];
fs.read( filedata, 0, (int)(fs.length) );
fs.close();response.addheader( "content-type", "application/vnd.ms-excel" );
string saveasfilename = "yoursaveasfilename";
saveasfilename = encode(saveasfilename);
//saveasfilename = httputility.urldecode(saveasfilename, system.text.encoding.getencoding("shift-jis"));
response.addheader("content-disposition", "inline;filename=" + saveasfilename);response.addheader("content-length", filedata.length.tostring() );
response.binarywrite( filedata );
response.end();
}
}
//方法二
public static void downloadfile( string filepath )
{
string str = httpcontext.current.request.servervariables[ "appl_physical_path" ];
string path = str + "www.111cn.net" + filepath;
fileinfo file = new fileinfo( path );
httpcontext.current.response.contentencoding = system.text.encoding.getencoding( "utf-8" ); //解决中文乱码
httpcontext.current.response.addheader( "content-disposition", "attachment; filename=" + httpcontext.current.server.urlencode( file.name ) ); //解决中文文件名乱码
httpcontext.current.response.addheader( "content-length", file.length.tostring() );
httpcontext.current.response.contenttype = "appliction/octet-stream";
httpcontext.current.response.writefile( file.fullname );
httpcontext.current.response.end();
}