用js 实现下载 <a href="#">点击下载此文件</a> 怎么实现 ? 指定的文件 比如 点击链接下载 a.pdf 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <a href="a.pdf">点击下载此文件</a> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%><!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 runat="server"> <title>无标题页</title> </head><body> <form id="form1" runat="server"> <div> <a href="#" onclick="location='?id=1';">下载</a> </div> </form></body></html>using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (Request.Params["id"] != null) { //可以先判断是不是从本页面点击下载的,不是的话就不准下载了 //string id = "1";//对应一个下载文件的编号 if (Request.Params["id"].ToString()=="1") DownLoad(@"e:\1.txt"); } } private void DownLoad(string filePath) { Response.Clear(); string fileName = Path.GetFileName(filePath); FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }} 如果指定是要下载 pdf 文件可以把Response.ContentType = "application/octet-stream";改成 Response.ContentType = "application/pdf";这样如果你的浏览器装了pdf文件查看器,就支持直接打开了,我们一般文件打印就是这样做的 恩,应该是我装了 pdf查看器 。就直接链接就可以了。 try catch中, Application.Run(new Form1());提示“未将对象引用设置到对象的实例”,无法提示哪行代码 如何给script脚本中变量赋值 关于MSCHART的问题 如何比较并且根据比较的在插入 数据库连接问题? 【求助】关于删除过期的目录 选项卡内控件怎样共享呢? c#有没有更快的方法可以删除文本中的某一行? 求解最高相似性 想写个类继承SocketAsyncEventArgs,但是遇到个事件绑定问题 菜鸟提问,快来啊! FCKeditor1 编辑器问题 用C#如何实现文本的居中,左对齐,右对齐和两端对齐
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick="location='?id=1';">下载</a>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["id"] != null)
{
//可以先判断是不是从本页面点击下载的,不是的话就不准下载了
//string id = "1";//对应一个下载文件的编号 if (Request.Params["id"].ToString()=="1")
DownLoad(@"e:\1.txt");
}
} private void DownLoad(string filePath)
{
Response.Clear();
string fileName = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
可以把Response.ContentType = "application/octet-stream";
改成 Response.ContentType = "application/pdf";这样如果你的浏览器装了pdf文件查看器,就支持直接打开了,我们一般文件打印就是这样做的