using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace SuAn { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { }
protected void Button1_Click(object sender, EventArgs e)
{ if (this.filepost.PostedFile.FileName == "") { Response.Write("上傳文件不能為空!"); return; } try { string Path = Server.MapPath("upload/");//設(shè)置服務(wù)器端路徑 string filePath = this.filepost.PostedFile.FileName;//獲取客戶端實際路徑 string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);//獲取文件名稱 string serverPath = Path + fileName;//上傳的文件保存到服務(wù)器端的路徑 System.Text.StringBuilder buider = new System.Text.StringBuilder();//上傳的文件信息:可變字符串 buider.Append("上傳文件的類型:"+this.filepost.PostedFile.ContentType.ToString()+""); buider.Append("客戶端文件地址:"+this.filepost.PostedFile.FileName+""); buider.Append("上傳文件名稱:"+fileName); buider.Append("上傳文件的擴展名:"+filePath.Substring(fileName.LastIndexOf(".")+1)); buider.Append("上傳文件的大?。?+this.filepost.PostedFile.ContentLength/1024+"k"+""); if (System.IO.File.Exists(serverPath)) { Response.Write("這個文件在服務(wù)器上已經(jīng)存在,請不要重復上傳!"); return; } string str = fileName.Substring(fileName.LastIndexOf(".")+1);//獲取文件后綴名 if (str == "jpg" || str == "rar" || str == "ppt") { Response.Write("對不起,該類型文件不能被上傳!"); return; } if (this.filepost.PostedFile.ContentLength >2048) { Response.Write("對不起,文件不能超過2K"); return; } this.filepost.PostedFile.SaveAs(serverPath);//上傳保存文件 this.lblMessage.Text = buider.ToString(); } catch(Exception ex) { Response.Write(ex.Message); } } } } |
|
來自: 修行的嘟嘟 > 《軟件開發(fā)》