javascript 实现文件下载

yanasdf789 2012-09-29 03:04:05
怎样在 asp.net 网站中实现文件下载,
我直接链接到图片地址,就打开一个页面,我需要的是下载对话框,怎么实现呢?
js 这么写
...全文
367 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanasdf789 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
js 这么写?
js实现不了这样的功能,你需要在asp.net写
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=xxx.txt");
Response.WriteFil……
[/Quote]

上诉代码可以实现弹出下载对话框,但是点击下载后,原页面就关闭了,怎样取消关闭啊
,<body>
<form >
<asp:button text="xiazai" onclick="download_click"
</form>
<body>


public void download_click(object sender,eventArgs e)
{
js 这么写?
js实现不了这样的功能,你需要在asp.net写
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=aa.txt");
string filename=Server.Path("~/aa.txt");
Response.WriteFile( filename)
Response.End();


}
孟子E章 2012-09-29
  • 打赏
  • 举报
回复
js 这么写?
js实现不了这样的功能,你需要在asp.net写
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=xxx.txt");
Response.WriteFile("路径")
Response.End();


这类代码网上很多了
king_1314 2012-09-29
  • 打赏
  • 举报
回复
看看下面的: 百度搜一下听多的
.
1 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=adTypeBinary

adodbStream.Open();

adodbStream.write(xmlHTTP.responseBody);

adodbStream.SaveToFile(strLocalURL,2);

adodbStream.Close();

adodbStream=null;

xmlHTTP=null;



}

catch(e)

{

window.confirm("下载URL出错!");

}

//window.confirm("下载完成.");

}

例如要下载文件的地址为:htpp://www.baidu.com/test.rar
我们可以使用window.open("htpp://www.baidu.com/test.rar"),
但是该方法在火狐上没有效果的,在IE浏览器上是可以的。

如果用window.location.href="htpp://www.baidu.com/test.rar";火狐有些版本是不支持的。

为了解决这个问题,我们可以换做另外一种方法
我们可以写成如下样式即可以了:
window.location="htpp://www.baidu.com/test.rar";


2 文件的下载到本地(默认)(ie 和火狐都可以)

function downloadFile(url)
{
try{
var elemIF = document.createElement("iframe");
elemIF.src = url;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
}catch(e){

}
}
js触发:
<div class="login_center"><a href="javascript:_login()"></a></div>
<input type="button" value="进入1" onClick = "downloadFile('http://wan.exe')">

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧