给你一段
if (e.CommandName == "Selectupdown")
{
string sqlupdown = "select * from contractside where id="+ int.Parse(e.Item.Cells[0].Text.Trim()) +"";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["sqlconnstring"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sqlupdown,conn);
SqlDataReader drupdow = cmd.ExecuteReader();
if (drupdow.Read())
{
Response.ContentType = "application/x-msdownload";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(drupdow["name"].ToString().Trim()) + ""+ drupdow["expandname"].ToString().Trim() +"");
Response.BinaryWrite((byte[])drupdow["side"]);
Response.Flush();
Response.End();
RegisterStartupScript("alert", "<script language='javascript'> alert('附件成功下载!')</script>");
}
drupdow.Close();
conn.Close();
}
if (e.CommandName == "Selectupdown")
{
string sqlupdown = "select * from contractside where id="+ int.Parse(e.Item.Cells[0].Text.Trim()) +"";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["sqlconnstring"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sqlupdown,conn);
SqlDataReader drupdow = cmd.ExecuteReader();
if (drupdow.Read())
{
Response.ContentType = "application/x-msdownload";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(drupdow["name"].ToString().Trim()) + ""+ drupdow["expandname"].ToString().Trim() +"");
Response.BinaryWrite((byte[])drupdow["side"]);
Response.Flush();
Response.End();
RegisterStartupScript("alert", "<script language='javascript'> alert('附件成功下载!')</script>");
}
drupdow.Close();
conn.Close();
}
解决方案 »
- asp初学,各位大虾帮忙一下。。
- radpanelbar 控件如何使用?
- visual studio 2005 调试问题
- 请教有开发经验的一个问题:? 随心谈````回答都有分
- 从没见的.net错误~!求助啊。
- 为什么我的web程序总会不定期出现HttpReject?
- 想请教大家一下visualbasic2008.net datetimepicker控件用不了是怎么回事?而且查看全部控件才能看到datetimepicker是灰
- 请问这两种去SESSION的方式有什么不同?
- 问两个问题......分数不多了,回的好可以舍命加分.
- 怎么在ASP与ASP.NET之间参数传递.
- 哪里有免费的或价廉的发短信的WEB SERVICE?
- 问各位高手,在VS.NET报表中怎样接收窗体输入框中的数据。
With Response
.Clear()
.ClearHeaders()
.Buffer = False
.AddHeader("Content-Disposition:", "attachment; filename=" & System.Web.HttpUtility.UrlEncode(oFile.FullName))
.AddHeader("Content-Length", oFile.Length.ToString())
.Charset = "GB2312"
.ContentType = "APPLICATION/vnd.ms-excel"
.ContentType = "application/octet-stream"
.WriteFile(oFile.FullName)
.Flush()
.End()
End With
string strFile = "e:\\abc.doc";
FileStream fs = new FileStream(strFile, 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.UrlEncode(File.Name)) ;
Response.BinaryWrite(bytes);
Response.End();
With Response
.Clear()
.ClearHeaders()
.Buffer = False
.AddHeader("Content-Disposition:", "attachment; filename=" & System.Web.HttpUtility.UrlEncode(oFile.FullName))
.AddHeader("Content-Length", oFile.Length.ToString())
.Charset = "GB2312"
.ContentType = "APPLICATION/vnd.ms-excel"
.WriteFile(oFile.FullName)
.Flush()
.End()
End With
刚才发多了一段
To force the browser to display the Download dialog, you need to set the Response object's ContentType property to application/octet-stream and add an HTTP Request header called Content-Disposition with the following value: attachment; filename="filename" where filename is the default filename that will be displayed in the Download dialog at the browser. Therefore, to send a file to the browser, the first thing to do is to write the following code:
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & filename & """")I will give you a example:
<%@ Import Namespace="System.IO"%>
<html>
<head>
<title>File Download</title>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim root As String = "C:\temp\"
Dim files() As String = Directory.GetFiles(root)
Dim sb As New StringBuilder(2048)
Dim f As String
For Each f In files
Dim filename As String = Path.GetFileName(f)
sb.Append("<br><a href=FileDownload.aspx?file=")
sb.Append(root).Append(Server.UrlEncode(filename))
sb.Append(">").Append(filename).Append("</a>")
Next
fileList.Text = sb.ToString()End Sub</script>
</head>
<body>
<form runat="server">
<asp:Label id="fileList" runat="server"/>
</form>
</body>
</html>
那我就要写成像 objDataGrid.Page.Response…… 这样了。这样的确可以实现下载功能,问题是这样访问Page会引起其他java script的问题,比如不允许访问window.event不知道有没有人遇到过这问题,是怎么解决的?
Public Sub OpenOrDownloadFile(ByVal FileName As String)
Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
'必须用URL路径
Dim Fname As String = "http://" & Request.UrlReferrer.Host + "/" + Request.Url.Segments(1) + "Exported/" + FileName
Dim sb As StringBuilder = New StringBuilder("<script language='javascript'>")
sb.Append("window.open('" + Fname + "',target='_blank','height=" + Session("ClientScreenHeight") + ",width=" + Session("ClientScreenWidth") + ",left=0,top=0,status=yes,toolbar=yes,menubar=yes,location=no,resizable=yes,scrollbars=yes')")
'sb.Append("{window.open('" + Fname + "',null,'height=" + Session("ClientScreenHeight") + ",width=" + Session("ClientScreenWidth") + ",left=0,top=0');}")
sb.Append("</script>")
context.Response.Write(sb.ToString())
End Sub
但通常提供文件链接的实现方法,在下载文件时,如图片文件,文本文件,HTML文件或Word文件等,浏览器会自动调用相关程序直接打开要下载的文件.
我想做到下载任何文件时都能弹出下载对话框,不知道有什么办法可以实现?如果想下载的话点右键就可以了,
象你说的那些文件图片文件,文本文件,HTML文件用户会不会一般想直接看看就可以了呢?
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-word";
Response.Charset = "";
this.EnableViewState = false;
Response.ContentEncoding =System.Text.Encoding.UTF8; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); //this.ClearControls(dg);
DataGrid1.RenderControl(oHtmlTextWriter);//dg是DataGrid Response.Write(oStringWriter.ToString()); Response.End();
"attachment; filename=""" & filename & """")关键就是这句,attachment告诉游览器下载,不是打开