call download(server.mappath("./test.jpg")) Function download(File) temp = Split(File, "\") filename = temp(UBound(temp)) Set s = CreateObject("adodb.stream") s.mode = 3 s.Type = 1 s.Open s.loadfromfile(File) data = s.Read If IsNull(data) Then response.Write "空" Else response.Clear Response.ContentType = "application/octet-stream" Response.AddHeader "Content-Disposition", "attachment; filename=" & filename response.binarywrite(data) End If Set s = Nothing End Function 找了一段代码,不太明白
超链接的目标是下载还是打开,是由浏览器设置决定的,浏览器则根据目标对象的 MIME TYPE 来决定该采用什么规则处理。因此,要限定浏览器的行为,就需要控制 http 协议头的 MIME TYPE。要实现这个目的,需要在服务器端用代码控制,原理就是这样,楼上几段代码的实现机理都是一样的。至于是直接下载还是提示后下载,仍然是由浏览器对不同 MIME TYPE 的处理设置决定的。这造成这样一个问题:你可以通过调整 MIME TYPE 的值,精确控制浏览器对超链接的反应,但这仅能保证在本机的精确行为,别人的机器不能保证和你的机器行为一致。
一部分下载网站是调用的迅雷的软件接口
还有一部分下载网站是使用的QQ旋风软件的接口。这些下载方式都有一个问题就是要安装一个非常大的软件。可能有些不方便。如果有比较小的插件就好了,因为有时侯下载的文件不大。都比较小。
Function download(File)
temp = Split(File, "\")
filename = temp(UBound(temp))
Set s = CreateObject("adodb.stream")
s.mode = 3
s.Type = 1
s.Open
s.loadfromfile(File)
data = s.Read
If IsNull(data) Then
response.Write "空"
Else
response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
response.binarywrite(data)
End If
Set s = Nothing
End Function
找了一段代码,不太明白
string FileName = url.Substring(url.LastIndexOf("/") + 1);
WebClient UrlFile = new WebClient(); HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
byte[] b = UrlFile.DownloadData(url);
HttpContext.Current.Response.BinaryWrite(b);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
IE的话,IE9也自带了。
FF也自带了。
GOOGLE也自带了。
但是一打开页面还没点击就提示下载了?该怎么完善一下?
我的全部代码:
<%@ CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<%Response.AddHeader "Content-Disposition","attachment; filename=Aladdin.jpg"%>
<body>
<img src="Aladdin.jpg" width="256" height="192" />
</body>
</html>
<iframe height="0" width="0" src="pic.jpg" name="saveImage" id="saveImage"></iframe>
<a href="#" onclick="saveImage.document.execCommand('saveAs');">Click Me</a>正好也要用,网上找的代码 不知道怎么样
同时感谢24楼!