我在登陆页放了个Window1窗体,然后进行校验密码对了可以下载为失效,如果直接把下载代码放在pageload事件又可以,放在按钮事件一点动静都没有
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using FineUI;
namespace Others
{
public partial class GetFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//DownFile();
//TransmitFile();
}
private void DownFile()
{
string fileName = ConfigurationManager.AppSettings["File"];// "项目申报表.docx";
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ConfigurationManager.AppSettings["Path"] + "\\" + ConfigurationManager.AppSettings["File"];
if (System.IO.File.Exists(filePath))
{
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
private void TransmitFile()
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
代码如下:
*/
string fileName = ConfigurationManager.AppSettings["File"];// "项目申报表.docx";
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ConfigurationManager.AppSettings["Path"] + "\\" + ConfigurationManager.AppSettings["File"];
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
//string filename = Server.MapPath("DownLoad/aaa.zip");
Response.TransmitFile(filePath);
}
private bool CheckLog()
{
return true;
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (CheckLog())
{
Label1.Text = "正在下载请稍后...";
DownFile();
// TransmitFile();
Window1.Hidden = true;
}
else
{
Alert.Show("用户名或密码错误!");
}
}
}
}
|