protected void cmdUpload_Click(object sender, EventArgs e)
{
// 检查文件是否提交;
if (Uploader.PostedFile.ContentLength != 0)
{
try
{
if (Uploader.PostedFile.ContentLength > 1048576)
{
// 限制文件大小,拒绝大的请求;
lblStatus.Text = "文件太大,请选择小于1M的文件上传!";
}
else
{
//获取UpLoad子目录的物理目录路径;
string destDir = Server.MapPath("./Upload");
// 从源文件的完整路径中抽取文件名;
string fileName = System.IO.Path.GetFileName( Uploader.PostedFile.FileName);
// 将目标目录与文件名相连;
string destPath = System.IO.Path.Combine(destDir, fileName);
// 保存文件到服务器;
Uploader.PostedFile.SaveAs(destPath);
lblStatus.Text = "文件路径为"+destPath+" 上传成功,谢谢您的上传!";
Image img = new Image();
img.Width = 300;
img.Height = 400;
img.ImageUrl = "~/Upload/"+fileName;
this.Controls.Add(img);
// Display the whole file content.
//StreamReader r = new StreamReader(Uploader.PostedFile.InputStream);
//lblStatus.Text = r.ReadToEnd();
//r.Close();
}
}
catch (Exception err)
{
lblStatus.Text = err.Message;
}
}