public string ReplyDocument_Upload(long customer, string customerName) { //新路径 string uploadFolderPath = Runbow.TWS.Common.Constants.UPLOAD_AMS_PATH; string targetPath = Path.Combine(uploadFolderPath, customerName, DateTime.Now.ToString("yyyy-MM")); DateTime createDate; string url = string.Empty, actualNameInServer = string.Empty, displayName = string.Empty, ext = string.Empty; IList <AMSUpload> amsUpload = new List <AMSUpload>(); if (string.IsNullOrEmpty(targetPath) || !Path.IsPathRooted(targetPath)) { return(new { msg = "程序出错!" }.ToJsonString()); } if (Request.Files.Count > 0) { HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; if (hpf.ContentLength > 0) { if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } displayName = Path.GetFileName(hpf.FileName); ext = Path.GetExtension(hpf.FileName); if (!ext.ToLower().Equals(".zip")) { return(new { msg = "批量上传,请用zip格式压缩" }.ToJsonString()); } actualNameInServer = UserInfo.ProjectName + DateTime.Now.ToString("yyyyMMddHHmmss") + ext; url = Path.Combine(targetPath, actualNameInServer); hpf.SaveAs(url); hpf.InputStream.Close(); if (ext.ToLower().Equals(".zip")) { IList <string> unZipedFileName = new List <string>(); ZipHelper.UnZip(url, targetPath, unZipedFileName); MyFile.Delete(url); unZipedFileName.Each((k, fileName) => { actualNameInServer = Path.GetFileName(fileName); ext = Path.GetExtension(fileName); displayName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(actualNameInServer)); createDate = DateTime.Now; amsUpload.Add(new AMSUpload() { FileName = displayName, FileType = ext, ServerName = actualNameInServer, FilePath = fileName, ProjectID = customer, ProjectName = customerName, OrderNo = "", Creator = UserInfo.Name, CreateTime = createDate, Updator = "", UpdateTime = createDate, Status = false }); }); } else { amsUpload.Add(new AMSUpload() { FileName = displayName, FileType = ext, ServerName = actualNameInServer, FilePath = url, ProjectID = UserInfo.ProjectID, ProjectName = UserInfo.ProjectName, OrderNo = "", Creator = UserInfo.Name, CreateTime = DateTime.Now, Updator = "", UpdateTime = DateTime.Now, Status = false }); } AMSUploadService service = new AMSUploadService(); //查询已存在的记录 Response <IEnumerable <AMSUpload> > resams = service.GetAMSUpload(new AddAMSUploadRequest() { amsUpload = amsUpload }); //执行新增修改操作 Response <IEnumerable <AMSUpload> > response = service.AddAMSUpload(new AddAMSUploadRequest() { amsUpload = amsUpload }); if (response.IsSuccess) { #region 除已存在记录的图片 if (resams.IsSuccess) { if (resams.Result != null) { foreach (AMSUpload a in resams.Result) { if (MyFile.Exists(a.FilePath)) { MyFile.Delete(a.FilePath); } } } } #endregion return(new { result = "批量上传文件成功!", IsSuccess = true }.ToJsonString()); } } return(new { result = "文件内容为空", IsSuccess = false }.ToJsonString()); } return(new { result = "请选择文件", IsSuccess = false }.ToJsonString()); }
public string AjaxUpload2(string gid, bool isMultiple, bool isCoverOld) {//, long customerID, string customerName string uploadFolderPath = Runbow.TWS.Common.Constants.UPLOAD_FOLDER_PATH; string targetPath = Path.Combine(uploadFolderPath, base.UserInfo.ProjectName, DateTime.Now.ToString("yyyy-MM")); DateTime createDate; string attachmentGroupID = gid, url = string.Empty, actualNameInServer = string.Empty, displayName = string.Empty, ext = string.Empty; IList <AMSUpload> amsUpload = new List <AMSUpload>(); // amsUpload.Add(new AMSUpload() { FileName = displayName, FileType = ext, ServerName = actualNameInServer, FilePath = fileName, ProjectID = customer, ProjectName = customerName, OrderNo = "", Creator = UserInfo.Name, CreateTime = createDate, Updator = "", UpdateTime = createDate, Status = false }); if (string.IsNullOrEmpty(targetPath) || !Path.IsPathRooted(targetPath)) { return(new { msg = "程序出错!" }.ToJsonString()); } try { if (Request.Files.Count > 0) { HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; if (hpf.ContentLength > 0) { if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } displayName = Path.GetFileName(hpf.FileName); ext = Path.GetExtension(hpf.FileName); if (isMultiple && !ext.ToLower().Equals(".zip")) { return(new { msg = "批量上传,请用zip格式压缩" }.ToJsonString()); } //actualNameInServer = displayName.Substring(0, displayName.Length - ext.Length + 1) + DateTime.Now.ToString("yyyyMMddHHmmss") + ext; actualNameInServer = UserInfo.ProjectName + DateTime.Now.ToString("yyyyMMddHHmmss") + ext; url = Path.Combine(targetPath, actualNameInServer); hpf.SaveAs(url); hpf.InputStream.Close(); if (ext.ToLower().Equals(".zip") && isMultiple) { IList <string> unZipedFileName = new List <string>(); ZipHelper.UnZip(url, targetPath, unZipedFileName); MyFile.Delete(url); unZipedFileName.Each((k, fileName) => { actualNameInServer = Path.GetFileName(fileName); ext = Path.GetExtension(fileName); //string groupID = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(actualNameInServer)); displayName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(actualNameInServer)); createDate = DateTime.Now; amsUpload.Add(new AMSUpload() { FileName = displayName, FileType = ext, ServerName = actualNameInServer, FilePath = fileName, ProjectID = UserInfo.ProjectID, ProjectName = UserInfo.ProjectName, OrderNo = "", Creator = UserInfo.Name, CreateTime = createDate, Updator = "", UpdateTime = createDate, Status = false }); }); } else { amsUpload.Add(new AMSUpload() { FileName = displayName, FileType = ext, ServerName = actualNameInServer, FilePath = url, ProjectID = UserInfo.ProjectID, ProjectName = UserInfo.ProjectName, OrderNo = "", Creator = UserInfo.Name, CreateTime = DateTime.Now, Updator = "", UpdateTime = DateTime.Now, Status = false }); } AMSUploadService service = new AMSUploadService(); Response <IEnumerable <AMSUpload> > response = service.AddAMSUpload(new AddAMSUploadRequest() { amsUpload = amsUpload, IsCoverOld = isCoverOld }); if (response.IsSuccess) { if (isMultiple) { return(new { msg = "批量上传文件成功", aids = response.Result.Select(a => a.ID), anms = response.Result.Select(a => a.ServerName), times = response.Result.Select(a => a.CreateTime), creators = response.Result.Select(a => a.Creator) }.ToJsonString()); } else { return(new { msg = "上传文件成功", gid = response.Result.First().ID, aid = response.Result.First().ID, anm = response.Result.First().ServerName, time = response.Result.First().CreateTime, creator = response.Result.First().Creator }.ToJsonString()); } } else { return(new { msg = "上传文件失败!" }.ToJsonString()); } } } return(new { msg = "文件无内容" }.ToJsonString()); } catch (Exception ex) { return(new { msg = ex.Message }.ToJsonString()); } }