读书人

html + javascript 怎么实现点击下载链

发布时间: 2012-04-04 16:38:51 作者: rapoo

html + javascript 如何实现点击下载链接或按钮弹出下载对话框
html + javascript 如何实现点击下载链接或按钮弹出下载对话框
我要下载的是 .excel .word .pdf .ppt文件

使用
<a href = "document/ProviderRecommend.xls" >下载</a>
来做,系统总是默认打开该文件,而不是弹出下载框。

用zip文件来做时候,
<a href = "document/ProviderRecommend.zip" >下载</a>
也是打开个窗口,在窗口中能见到该ProviderRecommend.xls文件,还是无法弹出下载对话框;

请高手指教!




[解决办法]
href 应该是超链接标签
[解决办法]
FTP
[解决办法]
我用的方法是提交到action中处理
如下:
if (action.equalsIgnoreCase("docatt")) {
//下载指定文公的附件
BufferedOutputStream bos = null;
String docid = request.getParameter("docid");
String filename=request.getParameter("filename");
StringBuffer sb = new StringBuffer(50);
sb.append("attachment; filename=");
sb.append(filename);
try {
if (null != docid && filename != null) {
response.setContentType("application/x-msdownload;charset=GB2312");
response.setHeader("Content-Disposition", new String(sb.toString()
.getBytes(), "ISO8859-1"));
String tmp = commonFunc.uploadpath + docid + "/" + filename;

FileInputStream fis = new FileInputStream(tmp);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
while (fis.read(buffer) != -1) {
bos.write(buffer);
}
bos.write(buffer, 0, buffer.length);

fis.close();
bos.close();
}
} catch (IOException e) {
log.error(e);
}
}


我也是和你一样的方法,不过我就没有空白页面,直接下载下来,你的文件不要用链接形式链接到需要的地址,而是点了链接以后产生onclick事件,然后再Javascript中提交form

______________________________________________________________

JS方法

</SCRIPT>
<script language=JavaScript>
function savepic(){
if(document.all.a1==null){
objIframe=document.createElement("IFRAME");
document.body.insertBefore(objIframe);
objIframe.outerHTML= "<iframe name=a1 style='width:0;hieght:0' src="+pic1.href+"></iframe>";
re=setTimeout("savepic()",1)
}
else{
clearTimeout(re)
pic = window.open(pic1.href,"a1")
pic.document.execCommand("SaveAs")
document.all.a1.removeNode(true)
}}
</script>

<a href="<%=pic%>" id=pic1 onclick="savepic();return false;" style="cursor:hand">
单击下载优惠券
</a>

[解决办法]
怎样点击直接下载
直接让客户端浏览器下载已知类型的文件
实现点击链接直接下载文件
点击后直接下载而不是将其打开
写下载链接的时候,对于txt,doc,xls等文本类型的文件,点击链接时怎样才能直接下载,而不打开呢
如何左键点击连接连接直接下载.txt文件


网上搜到提供的方法都是用fso stream读取文件,然后通过修改http headers的办法,比较麻烦,而且效率很低。




IIS里边有一项Custom HTTP Headers 的选项,可以直接在里边修改Content-disposition为 attachment即可。


例子:http://www.mianbi.com/book/a2165bec-a644-4ad3-b2ca-77a8662ff3f6.txt



Microsoft provides a way to override this "stream now" behavior at the server side using a custom HTTP Header in IIS. To add the custom header, launch Internet Information Services Manager and expand your Web sites to reveal the folder where multimedia content is stored. Right-click this folder and choose Properties. Click the HTTP Headers tab, followed by the Add button.



For the Custom header name type: Content-disposition
For the Custom header value type: attachment



Click OK, click Apply, click OK.

Visit one of the Web pages linking to your content download and test the links. You should be presented with the Save As dialog.

There is a catch to this solution. By setting the Content-disposition for the entire folder, your server will pop the Save As dialog box for any file called by a Web browser, including Web pages. There are two ways to solve this problem: only store media files in the folder where you modify the Content-disposition or set the attribute on-the-fly using code.

原文来自:http://www.jakeludington.com/windows_media_server_admin/20041209_using_custom_http_headers_to_prompt_for_video_file_downloads.html



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/educast/archive/2007/10/13/1823323.aspx
[解决办法]
Microsoft provides a way to override this "stream now" behavior at the server side using a custom HTTP Header in IIS. To add the custom header, launch Internet Information Services Manager and expand your Web sites to reveal the folder where multimedia content is stored. Right-click this folder and choose Properties. Click the HTTP Headers tab, followed by the Add button.



For the Custom header name type: Content-disposition
For the Custom header value type: attachment



Click OK, click Apply, click OK.

Visit one of the Web pages linking to your content download and test the links. You should be presented with the Save As dialog.

There is a catch to this solution. By setting the Content-disposition for the entire folder, your server will pop the Save As dialog box for any file called by a Web browser, including Web pages. There are two ways to solve this problem: only store media files in the folder where you modify the Content-disposition or set the attribute on-the-fly using code.



[解决办法]
[color=#FF6600]

VB.NET code
                    Response.ClearContent()                    Response.ContentType = "application/x-excel"                    Response.AppendHeader("Content-Disposition", "attachment; filename=Temp.xls")                    Response.TransmitFile(Server.MapPath(".") & ("/Temp/" & Request.UserHostAddress.ToString() & ".xls"))'此处指文件的路径                    Response.End()
[解决办法]

[解决办法]
target="_self" 在A标记加上这个。我试了一下能打开下载。
<a href="prototype1.4.doc" target="_self">test</a>

[解决办法]
路过帮顶
[解决办法]
在load事件里面写方法。
[解决办法]
你用的什么开发语言,如果是asp.net的话,那么你就用webclient对象实现

[解决办法]
用迅雷下载

<script src='http://pstatic.xunlei.com/js/webThunderDetect.js'></script>

<a href="#" onClick="javascript:OnDownloadClick('thunder://QUFodHRwOi8vMTIxLjEwLjE0MC4yMTYvd2FyMy8yMDA5MTEvMjAwOTExMDMwOTIwLnppcFpa','',location.href,'00192',2,'',4)">迅雷高速下载一</a>

试试。。。

读书人网 >CSS

热点推荐