//上传,返回附件id public int UpLoad(FileUpload file) { string filepath = "~/Uploads/"; BLHelper.BLLAttachment BLLattachment = new BLHelper.BLLAttachment(); if (file.HasFile) { if (file.PostedFile.ContentLength < 5120000) { string fileName = file.ShortFileName; string fileExtension = Path.GetExtension(fileName); if (!ValidateFileType(fileExtension)) { return(0); } if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(filepath)); } fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_"); string FileName = DateTime.Now.Ticks.ToString() + "_" + fileName; if (!BLLattachment.IsAttachmentName(fileName)) { Common.Entities.Attachment attachment = new Common.Entities.Attachment(); //向附件表中插入数据 attachment.FileName = fileName; attachment.FilePath = System.Web.HttpContext.Current.Server.MapPath(filepath) + FileName; file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(filepath) + FileName); BLLattachment.Insert(attachment); return(Convert.ToInt32(attachment.AttachmentID)); } else { //该文件名已存在 return(-1); } } else { //上传的文件不能大于50M return(-2); } } else { return(-3); } }
//上传大文件,返回附件id public int UpLoad(System.Web.UI.WebControls.FileUpload file) { BLHelper.BLLAttachment BLLattachment = new BLHelper.BLLAttachment(); foreach (UploadedFile files in RadUploadContext.Current.UploadedFiles) { string Path = System.Web.HttpContext.Current.Server.MapPath(@"Uploads"); string fileName = files.GetName().ToString(); string Filename = DateTime.Now.Ticks.ToString() + "_" + files.GetName().ToString(); //如果路径不存在,则创建 if (System.IO.Directory.Exists(Path) == false) { System.IO.Directory.CreateDirectory(Path); } if (!BLLattachment.IsAttachmentName(fileName)) { Common.Entities.Attachment attachment = new Common.Entities.Attachment(); //向附件表中插入数据 attachment.FileName = fileName; attachment.FilePath = Path + Filename; Path = Path + "/" + Filename; //保存 files.SaveAs(Path, true); BLLattachment.Insert(attachment); return(attachment.AttachmentID); } else { //该文件名已存在 return(0); } } //没存 return(-1); }
//文件上传 public FileUpLoad UpLoadFile(HtmlInputFile InputFile) { //文件路径 string filepath = "~/Uploads/"; FileUpLoad fp = new FileUpLoad(); string fileName, fileExtension; // //建立上传对象 // HttpPostedFile postedFile = InputFile.PostedFile; fileName = System.IO.Path.GetFileName(postedFile.FileName); fileExtension = System.IO.Path.GetExtension(fileName); if (fileName == "") { fp.Attachid = -3; return(fp); } //上传文件大小 int i = 100; if (InputFile.PostedFile.ContentLength > i * 1024 * 1024) { fp.Attachid = -2; return(fp); } //根据类型确定文件格式 if (!ValidateFileType(fileExtension)) { //无效的文件类型 fp.Attachid = -1; return(fp); } string phyPath = HttpContext.Current.Request.MapPath(filepath); //判断路径是否存在,若不存在则创建路径 DirectoryInfo upDir = new DirectoryInfo(phyPath); if (!upDir.Exists) { upDir.Create(); } // //保存文件 // try { BLHelper.BLLAttachment blat = new BLHelper.BLLAttachment(); if (!blat.IsAttachmentName(fileName)) { fp.FilePath = HttpContext.Current.Request.MapPath(filepath) + fileName; fp.FileExtension = fileExtension; fp.FileName = fileName; fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_"); string Filename = DateTime.Now.Ticks.ToString() + "_" + fileName; //插入附件表 Common.Entities.Attachment at = new Common.Entities.Attachment(); at.FileName = System.IO.Path.GetFileName(postedFile.FileName); at.FilePath = filepath + Filename; blat.Insert(at); fp.Attachid = at.AttachmentID; postedFile.SaveAs(phyPath + Filename); return(fp); } else { fp.Attachid = 0; return(fp); } } catch { throw new ApplicationException("上传失败!"); } }