ASP.NET下载文件出现提示框或者直接显示在浏览器中
ASP.NET下载文件出现提示框或者直接显示在浏览器中
技术交流 2008-06-20 11:44 阅读42 评论0
字号: 大大 中中 小小
1:出现文件下载提示框
string strFile="F:\\a.doc";//路径根据实际情况而定
if(!System.IO.File.Exists(strFile))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi=new FileInfo(strFile);
Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fi.Name)) ;
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tmpbyte=new byte[1024*8];
FileStream fs=fi.OpenRead();
int count;
while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();
2:直接在浏览器中打开
string strFile="F:\\a.doc";//路径根据实际情况而定
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/msword";
Response.WriteFile(strFile);
3:封装成类的文件下载方法的写法
///
/// 在页面中显示下载对话框并下载指定的文件,webPage为页面对象引用(一般赋值Page),filePath为下载文件虚拟路径,fileName为对话框中显示的文件名
///
public static void DownloadFile(Page webPage, string filePath, string fileName)
{
HttpResponse Response = webPage.Response;
FileInfo aFile = new FileInfo(webPage.Server.MapPath(filePath));
Response.Clear();
Response.ClearHeaders();
Response.BufferOutp
相关文档:
欢迎拍砖,共同进步!!!
1、在compilation 下,请设置debug=false ,如下:
default Language="c#" debug="false">
2、请使用Server.Transfer代替Response.Redirect。
3、使用Validator控件,请要经常检查Page.IsValid。
4、请使用foreach循环,而不是为字符串迭代循环。
5、请使用客户端验证 ......
1. 打开新的窗口并传送参数: //传送参数:
response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="++"’)</script>")
//接收参数:
string a = Request.QueryString("id");
string b = Request.QueryString("id1");
2.为按钮添加对话框
Button1 ......
本文翻译自:Mixing Forms and Windows Security in ASP.NET
摘要:ASP.NET开发人员曾经问到过如何使用Forms和Windows混合验证。Paul Wilson提供了一个解决方案来获得Windows用户名,或者,将用户转向登录页面。
简介
我曾经遇到很多ASP.NET开发人员问到如何使用Forms和Windows混合验证。通常的回答是:“ASP.NET不 ......
1. 使用QueryString变量
QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。下面是一个例子:
a.aspx的C#代码
private void Button1_Click(object se ......