public partial class _Default :System.Web.UI.Page
{
protectedvoid Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack){
stringisDownLoad = Request.QueryString["isDownLoad"];
if(isDownLoad != null && "true".Equals(isDownLoad))
{
//文件下载
stringfileName = Request.QueryString["fName"];
stringfilePath = Server.MapPath("/") + "\\Image\\Upload\\" + fileName;
FileInfofInfo = new FileInfo(filePath);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, Encoding.UTF8));
Response.AppendHeader("Content-Length",fInfo.Length.ToString());
Response.WriteFile(filePath);
Response.Flush();
}
}
}
//文件上传
protectedvoid btnUplaod_Click(objectsender, EventArgs e)
{
lblFileName.Text = "";
if(fileUploadText.HasFile)
{
HttpPostedFilepostedFile = fileUploadText.PostedFile;
stringfilePath = Server.MapPath("/")+"\\Image\\Upload\\";
stringfilename = postedFile.FileName;
if(filename.Contains("\\"))
{
filename =filename.Substring(filename.LastIndexOf("\\")+1);
}
lblFileName.Text = filename;
postedFile.SaveAs(filePath +filename);
}
else{
Response.Write("<script>alert('请选择文件’')</script>");
}
}
//查询所有的待下载文件
protectedvoid btn_searchDocument_Click(object sender, EventArgse)
{
stringfilePath = Server.MapPath(".")+"\\Image\\Upload\\";
string[] files = Directory.GetFiles(filePath);
foreach(string fileName infiles){
//截取路径是为了兼容火狐和IE
string tempFileName =fileName.Substring(fileName.LastIndexOf("\\")+ 1);
lblFile.Text += "<ahref='Default.aspx?isDownLoad=true&fName="+tempFileName+"'>"+tempFileName + "</a><br/>";
}
}
}