excel文件不提示下载,而是在浏览器中打开?
方法一: <%response.contenttype = "application/octet-stream "response.addheader "content-d**position ", "attachment; filename=xx.excel "response.binarywrite getbinaryfile( "c:/xxxxx/xx.excel ")function getbinaryfile(filespec)dim adtypebinaryadtypebinary = 1dim ostreamset ostream = server.createobject( "adodb.stream ")ostream.openostream.type = adtypebinaryostream.loadfromfile filespecgetbinaryfile= ostream.readset ostream=nothingend function%> 方法二:直接让客户端浏览器下载已知类型的文件 web开发人员都有过这样的疑问,如何让一个文件,尤其是一个已知类型的文件,发送到客户端,直接提示让浏览者下载,而不是用与它相关联的程序打开。以前我们最常用的办法就是把这样的文件加到链接上,这样可以让浏览者通过点击鼠标右键的目标另存为来下载所链接的文件。但是,这样有两个不足的地方: 一是:如果浏览器能够识别已下载文件的扩展名,则浏览器就会激活该扩展名所关联的程序来打开所下载的文件。比如:在windows**上,如果用户点击的链接链接的是一个“.doc”文件的话,那么,浏览器就会启动microsoft word应用程序来打开它。 二是:如果采用链接的办法的话,任何能看到该链接的人都可以下载该文件,你虽然也可以对所下载的文件进行权限设置,但那样做也不是很方便的。有时候我们需要更为灵活和富有弹性的方式,下面的程序能够很方便地克服以上两方面的不足。 这种办法是可靠的,但你必须记住:没有授权的用户不能够通过在浏览器地址栏里输入文件的url来取得该文件的下载权。所以,要下载的文件应该放到虚拟目录之外的一个目录里,比如:如果你的虚拟目录是c:/mengxianhui/tomcat4/website/myapp的话,那么,存放在该目录和该目录下的任何子目录下所有文件对因特网上的任何用户都是可见的。要直接下载一个文件,我们需要做两件事,第一件事是:设定响应的内容类为“application/octet-stream”,大小写无关。第二件事是:设置http的响应头名字为:content-d**position,设定值为:attachment; filename = thefilename。这里的thefilename就是出现在文件下载对话框里的默认文件名,通常和所下载的文件名字相同,但也可以不同。下面,我们就平常最常用的jsp和asp页面来举一个实际应用的例子。 testfiledownload.jsp页面的例子: <% // 得到文件名字和路径 string filename = "mengxianhuidoctest.doc "; string filepath = "d:// "; // 设置响应头和下载保存的文件名 response.setcontenttype( "application/octet-stream "); response.setheader( "content-d**position ", "attachment; filename=/ " " + filename + "/ " "); // 打开指定文件的流信息 j**a.io.fileinputstream fileinputstream = new j**a.io.fileinputstream(filepath + filename); // 写出流信息 int i; while ((i=fileinputstream.read()) != -1) { out.write(i); } fileinputstream.close(); out.close(); %> 值得注意的是:在要下载的文件内容里,除了文件的内容之外,不应该再附加有其它任何的字符,包括空格和回车换行符。我们有时在编写代码的时候,为了使代码清晰可读,往往会添加一些空格、制表符或者回车换行符,这样虽然看起来比较清晰,但有时可能会得不到正确的结果。比如: <%@ page import= "j**a.io.* " %> <jsp:usebean id= "mybeanfrommengxianhui " scope= "page " class= "com.mengxianhui.downloadbean " /> 应该写成这样: <%@ page import= "j**a.io.* " %> <jsp:usebean id= "mybeanfrommengxianhui " scope= "page " class= "com.mengxianhui.downloadbean " /> testfiledownload.asp页面的例子: 在asp里,没有提供从文件读取文件流信息的方法,因此,为了得到文件的流信息,我们必须借助其他的工具,最简单的就是编写一个vb或c的dll组件,让组件返回文件的流信息。下面是一个用vb编写的dll的例子,工程名字为mengxhfiledownload,类模块的名字为binreadfromfile,类方法readbinfromfile如下: function readbinfromfile(byval bfilename as string) as variant dim fl as long dim filenum as long dim binbyte() as byte dim binfilestr as string on error goto errhandler filenum = freefile open bfilename for binary as #filenum fl = filelen(bfilename) redim binbyte(fl) get #filenum, , binbyte close #filenum readbinfromfile = binbyte exit function errhandler: exit function end function 把上面的代码编译成mengxhfiledownload.dll,然后注册即可使用。下面以直接下载一个when a man loves a woman.mp3的mp3文件为例子,我们要编写的asp脚本代码如下: <%@ language=vbscript %> <% response.buffer = true response.contenttype = "application/octet-stream " response.addheader "content-d**position ", "attachment;filename=when a man loves a woman.mp3 " dim varstream, omyobject set omyobject = server.createobject( "mengxhfiledownload.binreadfromfile ") varstream = omyobject.readbinfromfile( "e:/mengxianhui/mp3/when a man loves a woman.mp3 ") response.binarywrite(varstream) set omyobject = nothing response.end %> 当运行上面的testfiledownload.asp文件时,浏览器会弹出一个文件下载的对话框,提示我们下载,而不是用默认的mp3播放器打开。 这种方法也可以把我们的asp页面生成的html源代码保存成一个文件,下面的代码会提示你把asp执行的结果保存成test.htm文件。具体的方法是: <% response.contenttype = "application/octet-stream " response.addheader "content-d**position ", "attachment;filename=test.htm " response.write " <div style= 'background-color:n**y;color:#ffffff '> 测试 </div> " response.write " <a href= ' '> " response.write " <img src= '/back.jpg '> 【孟宪会之精彩世界】 </a> " response.end %> 当文件数目很少时,也可以直接在服务器端进行设置,让这些文件直接下载。具体做法是:在internet服务管理器里,选“属性”项,然后选“http headers”标签页进行设置即可!! 方法三:用js直接操作function downurl(strremoteurl,strlocalurl){try{var xmlhttp=new activexobject( "microsoft.xmlhttp ");xmlhttp.open( "get ",strremoteurl,false);xmlhttp.send();var adodbstream=new activexobject( "adodb.stream ");adodbstream.type=1;//1=adtypebinaryadodbstream.open();adodbstream.write(xmlhttp.responsebody);adodbstream.s**etofile(strlocalurl,2);adodbstream.close();adodbstream=**;xmlhttp=**;//openfile(strlocalurl);}catch(e){window.confirm( "下载url出错! ");}//window.confirm( "下载完成. ");} 20210311