ASP.NET 中的上传控件使用_[Asp.Net教程] using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
/// /// MyUpload 的摘要说明 /// public class MyUpload { private System.Web.HttpPostedFile postedFile = null; private string savePath = ""; private string[] extension; private int fileLength = 0; public string[] filestype ={ "txt", "doc", "gif" }; public MyUpload() { // // TOD 在此处添加构造函数逻辑 // } public string Help { get { string helpstring; helpstring = "MyUpload myUpload=new MyUpload(); //构造函数"; helpstring += "myUpload.PostedFile=file1.PostedFile;//设置要上传的文件"; helpstring += "myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\"; helpstring += "myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k"; helpstring += "myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt"; helpstring += "label1.Text=myUpload.Upload();//开始上传,并显示上传结果"; helpstring += ""; return helpstring; }
}
public System.Web.HttpPostedFile PostedFile { get { return postedFile; } set { postedFile = value; } }
public string SavePath { get { if (savePath != "") return savePath; return "c:\\"; } set { savePath = HttpContext.Current.Server.MapPath("../upimages/") + value;/////////////////////注意这里选取上传文件路径,可以修改成传递参数类型的 } }
public int FileLength { get { if (fileLength != 0) return fileLength; return 1024; } set { fileLength = value * 1024; } }
public string[] Extension { get { if (extension != null) return extension; return filestype; } set { extension = value; } }
public string PathToName(string path) { int pos = path.LastIndexOf("\\"); return path.Substring(pos + 1); }
public string Upload() { int biaoshi = 0; if (PostedFile.FileName != "") { try { string fileName = PathToName(PostedFile.FileName); for (int i = 0; i < Extension.Length; i++) { if (fileName.EndsWith(Extension[i])) { biaoshi = 1; } } if (biaoshi != 1) { string errs = ""; for (int i = 0; i < Extension.Length; i++) { errs = errs + Extension[i] + "/"; } return "您必须选择" + errs + "类型的文件!"; } if (PostedFile.ContentLength > FileLength) return "文件太大,超过了限定值!"; PostedFile.SaveAs(SavePath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4)); return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4); } catch (System.Exception exc) { return exc.Message; } } return "请选择文件后再进行上传!"; } }
////////////////////使用例子,直接复制没有修改///////////////////// string[] filestype ={ "gif", "jpg", "png" }; MyUpload myload = new MyUpload(); myload.PostedFile = File1.PostedFile; myload.SavePath = ""; myload.FileLength = 1000; myload.Extension = filestype; Label1.Text = myload.Upload(); if (Label1.Text.StartsWith("200")) { Label2.Text = "upimages/" + Label1.Text;
Label1.Text = "上传更新成功!"; }
来源:CSDN
|