HTML code
<% @ Page Language = " C# " %> <% @ Import Namespace = " System.IO " %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < script runat ="server" > private void Page_Load(object sender, System.EventArgs e) { if ( ! IsPostBack) { this .ImageButton1.Enabled = false ; this .LinkButton1.Visible = false ; } } protected void Button1_Click(object sender, EventArgs e) { if ( this .FileUpload1.HasFile) { // 若选中了上传文件则将文件保存到服务器上的"Upload"文件夹 FileUpload1.SaveAs(Server.MapPath( " Upload " ) + @ " /" + Path.GetFileName(this.FileUpload1.FileName)); this.Label1.Text = Path.GetFileName(this.FileUpload1.FileName); this.LinkButton1.Text += this.Label1.Text; this.LinkButton1.CommandArgument = this.Label1.Text; this.LinkButton1.Visible = true; this.ImageButton1.Enabled = true; } else { this.Label1.Text = " 请选择要上传的文件 " ; } } protected void LinkButton1_Command(object sender, CommandEventArgs e) { string fileName = (string)e.CommandArgument; //客户端保存的文件名 string filePath = Server.MapPath( " Upload " ) + @ " / " + fileName;//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = " application / octet-stream"; // 通知浏览器下载文件而不是打开 Response.AddHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(filePath, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { string f = Request.MapPath( " Upload " ) + @ " /" + this.Label1.Text; try { if (File.Exists(f)) { File.Delete(f); Response.Write( " 删除成功! " ); } } catch { Response.Write( " 删除失败! " ); } } </script> <html xmlns= " http: // www.w3.org/1999/xhtml"> < head runat = " server " > < title > 无标题页 < / title> < / head> < body > < form id = " form1 " runat = " server " > < div > < asp:FileUpload ID = " FileUpload1 " runat = " server " / > < asp:Button ID = " Button1 " runat = " server " Text = " Upload " OnClick = " Button1_Click " / > < asp:Label ID = " Label1 " runat = " server " >< / asp:Label> < asp:LinkButton ID = " LinkButton1 " runat = " server " Text = " 下载 " OnCommand = " LinkButton1_Command " >< / asp:LinkButton> < asp:ImageButton ID = " ImageButton1 " runat = " server " ImageUrl = "" OnClick = " ImageButton1_Click " / > < / div> < / form> < / body> < / html>