如何实现在点击下载链接时,直接弹出一个下载的窗口.而不是在新的窗口打开这个PDF文件?
网上有断代码,但我不知道怎么用昨天刚刚找到的,直接粘下来,存成asp文件就行了!!
<%
Response.Buffer = true
Response.Clear
dim url
Dim fso,fl,flsize
dim Dname
Dim objStream,ContentType,flName,isre,url1
'*********************************************调用时传入的下载文件名
Dname=trim(request("n"))
'******************************************************************
If Dname<>"" Then
'******************************下载文件存放的服务端目录
url=server.MapPath("/")&"\"&Dname
'***************************************************
End If
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set fl=fso.getfile(url)
flsize=fl.size
flName=fl.name
Set fl=Nothing
Set fso=Nothing
%>
<%
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile url
Select Case lcase(Right(flName, 4))
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select
Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
Response.AddHeader "Content-Length", flsize
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite objStream.Read
Response.Flush
response.Clear()
objStream.Close
Set objStream = Nothing
%>
--------------------编程问答-------------------- 链接直接指向文件不行吗
--------------------编程问答-------------------- 直接用href的话 pdf txt 还有些文件 只要你机器上安装了能打开他们的软件,他就会在页面中直接打开,而不弹出那个保存 打开对话框, 解决的方法是你用一个空白页面例如down.aspx,在这个页面的page-load或page_init(这个自己写一个方法)写上下载的代码就可以了
因为我刚做过 呵呵 --------------------编程问答-------------------- js也可以解决
http://hexun.com/zjysky 里面博客里面的js技术中有个下载的东西。你看看 --------------------编程问答-------------------- 要强制下载的话,
需要添加 附件的HTTP头.
也就是这句:
Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
知道这个..
不管你是什么语言.只需要在输出内容前,先发送这个头就行了. --------------------编程问答-------------------- 给你段代码吧!不过是asp.net的
//打开下载与保存的下载对话框的代码
string filename = "/doc/infotable.doc";
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode("加盟商家表格.doc", System.Text.Encoding.UTF8));
Response.WriteFile(filename);
Response.End();
就这么简单!^_^ --------------------编程问答-------------------- Response.AddHeader("Content-Disposition", "attachment;FileName=123" --------------------编程问答-------------------- up --------------------编程问答-------------------- 到这里看看,记得点一下广告,再到主页点一下GOOGLE提供的广告,分不要也行拉,
http://www.ebookit.cn/302.html --------------------编程问答-------------------- 该response的头信息就可以了。
--------------------编程问答-------------------- 直接用href的怎么写? --------------------编程问答-------------------- 页面上代码:
<a href="wjgl_jlwjlist.aspx?file=<%#Eval("FILE_SOURCEFILE")%>" target="mainFrame">
<img src="images/main_center04.jpg" width="17" height="16" border="0" alt="下载" /></a>
后台接收:
string file = Request.QueryString["file"] == null ? "0" : Request.QueryString["file"].ToString();
if (file != "0")
{
FileName = file;
//调用下载方法
xiazai(FileName);
}
/// <summary>
/// 下载pdf
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string xiazai(string FileName)
{
//打开要下载的文件
//System.IO.FileStream r = new System.IO.FileStream((FileName), System.IO.FileMode.Open);
System.IO.FileStream r = new System.IO.FileStream(Server.MapPath(FileName), System.IO.FileMode.Open);
//设置基本信息
Response.Buffer = false;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
string FileName1 = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);//解决文件名乱码
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName1));
Response.AddHeader("Content-Length", r.Length.ToString());
// string strTemp = System.Web.HttpUtility.UrlEncode(strName, System.Text.Encoding.UTF8);//解决文件名乱码
while (true)
{
//开辟缓冲区空间
byte[] buffer = new byte[1024];
//读取文件的数据
int leng = r.Read(buffer, 0, 1024);
if (leng == 0)//到文件尾,结束
break;
if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
Response.BinaryWrite(buffer);
else
{
//读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte[] b = new byte[leng];
for (int i = 0; i < leng; i++)
b[i] = buffer[i];
Response.BinaryWrite(b);
}
}
r.Close();//关闭下载文件
Response.End();//结束文件下载
return FileName;
}
试试吧
--------------------编程问答--------------------
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName1));
要下载,这个部分不能错,要用“attachment”,千万别用“Inline”。Inline是在浏览器中直接打开。
补充:.NET技术 , ASP.NET