private UploadMessage _uploadFile(string companyID, string fileFolderName) { if (HttpContext.Current.Request.Files.AllKeys.Any()) { string fileSubFolder = "/files/" + fileFolderName + "/"; FileExtension[] allowFileExtension = { FileExtension.BMP, FileExtension.DOC, FileExtension.DOCX, FileExtension.XLS,FileExtension.XLSX, FileExtension.PDF, FileExtension.GIF, FileExtension.JPG,FileExtension.PNG }; if (!FileValidation.IsAllowedExtension(HttpContext.Current.Request.Files["file"], allowFileExtension)) { return(errorMsg("不接受的文件格式")); } bool isImage = FileValidation.IsImageExtension(HttpContext.Current.Request.Files["file"]); // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES. string folderPath = WebApiApplication.UploadFolderPath; if (folderPath.StartsWith("~") || folderPath.StartsWith(".")) //相对路径 { folderPath = HttpContext.Current.Server.MapPath(folderPath + "/files/" + companyID); } else { folderPath = folderPath + fileSubFolder + companyID; } string yearMonth = DateTime.Now.ToString("yyyyMM"); folderPath = Path.Combine(folderPath, yearMonth); //folderPath = folderPath.Replace(".WebAPI", ".Web").Replace("UploadedDocuments", "content/images"); bool folderExists = Directory.Exists(folderPath); if (!folderExists) { Directory.CreateDirectory(folderPath); } HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files["file"]; string fileType = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(".")); string fileName = Guid.NewGuid() + "-" + DateTime.Now.ToString("ddHHmmssff"); string fileSavePath = Path.Combine(folderPath, fileName + fileType); try { httpPostedFile.SaveAs(fileSavePath); } catch (ApplicationException ex) { return(errorMsg(ex.Message)); } if (isImage) { string originalFolderPath = folderPath; string thumbnailFolderPath = Path.Combine(folderPath, "thumbnail"); Image originalImage = StreamHelper.ImagePath2Img(fileSavePath); string fileThumbnailSavePath = Path.Combine(thumbnailFolderPath, fileName + fileType); folderExists = Directory.Exists(thumbnailFolderPath); if (!folderExists) { Directory.CreateDirectory(thumbnailFolderPath); } Image newImage = ImageHelper.GetThumbNailImage(originalImage, 320, 320); newImage.Save(fileThumbnailSavePath); UploadMessage uploadMessageImg = new UploadMessage() { OriginName = httpPostedFile.FileName, FileName = fileName, FileSize = httpPostedFile.ContentLength, FileType = fileType, DiskUploadPath = fileThumbnailSavePath, UploadFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + "/thumbnail/" + fileName + fileType, IsImage = true, HasThumbnail = true, OriginDiskUploadPath = fileSavePath, OriginFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, }; return(uploadMessageImg); } UploadMessage uploadMessage = new UploadMessage() { OriginName = httpPostedFile.FileName, FileName = fileName, FileSize = httpPostedFile.ContentLength, FileType = fileType, DiskUploadPath = fileSavePath, UploadFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, IsImage = false, HasThumbnail = false, OriginDiskUploadPath = fileSavePath, OriginFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, }; return(uploadMessage); } return(errorMsg("没有上传文件")); }