62,046
社区成员
发帖
与我相关
我的任务
分享
protected void Button1_Click(object sender, EventArgs e)
{
//1
string script = string.Format(CultureInfo.CurrentCulture, "<script>alert('{0}');</script>", "弹出框");
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "提示", script);
//2
Response.Write("<script type='text/javascript'>alert('ssss');</script>");
}
/// <summary>
/// 下载指定路径的文件
/// </summary>
/// <param name="filePath">相对路径文件,如"~\Upload\Test.xls"</param>
public static void DownloadFile(string filePath)
{
string path = HttpContext.Current.Server.MapPath(filePath);
FileInfo toDownload = new FileInfo(path);
if (toDownload.Exists)
{
const int chunkSize = 10000;
byte[] buffer = new byte[chunkSize];
HttpContext.Current.Response.Clear();
using (FileStream stream = File.OpenRead(path))
{
long dataLengthToRead = stream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(toDownload.Name));
while (dataLengthToRead > 0 && HttpContext.Current.Response.IsClientConnected)
{
int lengthRead = stream.Read(buffer, 0, chunkSize);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
HttpContext.Current.Response.Close();
}
}
public static void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
这个代码也经历了很多年 很多版本的IE也没什么问题..
public static void show(Page page, string str)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script language='javascript'>alert('" + str + "');</script>");
}
上面的代码从6运行到11 (IE) 并且用了5年以上 没问题..