上传文件[原创]


所属类别:Asp

文章作者:未知

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


/*Descript:上传文件Author:Blue.DreamDate:2004-09-21 22:21 */namespace BDStudio.Common{using System;using System.IO;using System.Web;/// /// 上传单个文件/// public class UpLoadFile{private string[] AllowFileType;//所允许的文件类型private double FileLength;//所允许的文件大小(KB)private string SavePath;//文件的存储路径private string SaveFile;//上传后的文件名private string Error;//存储错误信息private string FileExtesion;//上传文件的扩展名/// /// 构造函数/// /// 允许的文件类型,多个以","分开/// 文件大小/// 保存路径public UpLoadFile(string allFileType,double fileLength,string savePath){char[] sp = {','};AllowFileType = allFileType.Split(sp);FileLength = fileLength;SavePath = savePath;}/// /// 返回生成的文件名/// public string FileName{get{return SaveFile;}}/// /// 返回出错信息/// public string ErrorMessage{get{return Error;}}/// /// 根据SavePath,生成文件名/// /// private string MakeFileName(string fileType){string file = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+this.FileExtesion;for(; File.Exists(file);){file = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+this.FileExtesion;}return file;}/// /// 检查文件类型/// /// MIME内容/// private bool CheckFileExt(string fileEx){bool result = false;for(int i = 0; i < this.AllowFileType.Length; i++){if(fileEx.IndexOf(this.AllowFileType[i].ToLower()) > -1){result = true;break;}}return result;}public bool UpLoad(){bool result = true;System.Web.HttpFileCollection objFiles = System.Web.HttpContext.Current.Request.Files;System.Web.HttpPostedFile objFile = objFiles[0];try{//查看文件长度if(objFile.ContentLength > (this.FileLength)){this.Error = "文件大小超出范允许的范围!";return false;}string fileName = Path.GetFileName(objFile.FileName);this.FileExtesion = Path.GetExtension(fileName);if(!CheckFileExt(this.FileExtesion.ToLower())){this.Error = "文件类型"+this.FileExtesion+"不允许!";return false;}//取得要保存的文件名string UpFile = this.MakeFileName(this.FileExtesion);//保存文件objFile.SaveAs(UpFile);//返回文件名this.SaveFile = Path.GetFileName(UpFile);}catch(Exception e){result = false;this.Error = e.Message;}return result;}}}关闭本页

相关信息

· FlashGet网络快车应用技巧招招妙

· Linux和Windows双操作系统Grub丢失恢复

· SMPlayer v0.6.5.1发布

· JSP入门初级教程之JSP概述








....

81133 7611