/// <summary> /// 上传信息附件 /// </summary> /// <returns></returns> public async Task <object> UploadFiles(long communityId, [SwaggerFileUpload] string messageFile) { Community.Community community = null; App app = null; community = await _communityManager.CommunityRepository.FirstOrDefaultAsync(communityId); if (app == null && community != null) { app = await _appManager.AppRepository.FirstOrDefaultAsync(community.AppId); } List <string> fileArray = new List <string>(); var files = HttpContext.Current.Request.Files; for (int i = 0; i < files.Count; i++) { var file = files[i]; var fileName = messageFile; if (string.IsNullOrEmpty(fileName)) { fileName = DateTime.Now.Ticks.ToString(); } fileName = fileName + Path.GetExtension(file.FileName); var filePath = PathUtils.Combine(EFileUploadTypeUtils.GetFileUploadPath(EFileUploadType.AppCommon.ToString(), _appFolders, app), "Report", fileName); var relateFileUrl = filePath.Replace(System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }), string.Empty); DirectoryUtils.CreateDirectoryIfNotExists(filePath); file.SaveAs(filePath); fileArray.Add(relateFileUrl); } return(new { BaseUrl = HttpContext.Current.Request.Url.Host, Files = fileArray }); }
public async Task <string> FileUpload(FileUpload <FileUploadInput> input) { try { //Check input if (HttpContext.Current.Request.Files.Count <= 0 || HttpContext.Current.Request.Files[0] == null) { throw new UserFriendlyException(L("FIle_Upload_Error")); } var file = HttpContext.Current.Request.Files[0]; var fileInfo = new FileInfo(file.FileName); if (input.Value.MaxFileLength != 0 && file.ContentLength > input.Value.MaxFileLength) // 5242880 = 1MB { throw new UserFriendlyException(L("FIle_Upload_Warn_SizeLimit")); } //Check file type var fileExtension = fileInfo.Extension; if (input.Value.AllowFileExtensionArray.Count > 0 && !input.Value.AllowFileExtensionArray.Contains(fileExtension)) { throw new ApplicationException(L("FIle_Upload_Warn_ExtensionLimit")); } Community community = null; App app = null; if (input.Value.CommunityId.HasValue) { community = await _communityManager.CommunityRepository.FirstOrDefaultAsync(input.Value.CommunityId.Value); } else if (input.Value.HomeOwerId.HasValue) { var homeOwer = await _homeOwerManager.HomeOwerRepository.FirstOrDefaultAsync(input.Value.HomeOwerId.Value); if (homeOwer != null) { community = await _communityManager.CommunityRepository.FirstOrDefaultAsync(homeOwer.CommunityId); } } if (app == null && community != null) { app = await _appManager.AppRepository.FirstOrDefaultAsync(community.AppId); } //Save new picture //string fileName = string.Format("{0}{1}", Guid.NewGuid(), fileInfo.Extension); var filePath = EFileUploadTypeUtils.GetFileUploadPath(input.Value.FileUploadType, _appFolders, app); var relateFileUrl = PathUtils.Combine(filePath.Replace(System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }), string.Empty), input.FileName); DirectoryUtils.CreateDirectoryIfNotExists(filePath); input.Save(filePath); return(relateFileUrl); } catch (Exception ex) { throw ex; } }
public async virtual Task <IHttpActionResult> UploadFiles(string userName, string token, [SwaggerFileUpload] string reportFile = null) { base.AuthUser(); //验证是否是 multipart/form-data if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } var tenantId = base.User.TenantId; var homeOwerId = base.User.HomeOwerId; var communityId = base.User.CommunityId; using (CurrentUnitOfWork.SetTenantId(tenantId)) { var homeOwer = await _homeOwerManager.HomeOwerRepository.FirstOrDefaultAsync(homeOwerId.Value); if (homeOwer == null) { throw ErrorCodeTypeUtils.ThrowError(ErrorCodeType.HomeOwerNotExists); } Community community = null; App app = null; community = await _communityManager.CommunityRepository.FirstOrDefaultAsync(communityId.Value); if (app == null && community != null) { app = await _appManager.AppRepository.FirstOrDefaultAsync(community.AppId); } List <string> fileArray = new List <string>(); var files = HttpContext.Current.Request.Files; //保存reportFile, key = reportFile_file //var file = files["reportFile_file"]; //var fileName = reportFile; //if (string.IsNullOrEmpty(fileName)) // reportFile = DateTime.Now.Ticks.ToString(); //fileName = fileName + Path.GetExtension(file.FileName); //var filePath = PathUtils.Combine(EFileUploadTypeUtils.GetFileUploadPath(EFileUploadType.AppCommon.ToString(), _appFolders, app), fileName); //var relateFileUrl = filePath.Replace(System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }), string.Empty); //DirectoryUtils.CreateDirectoryIfNotExists(filePath); //file.SaveAs(filePath); //fileArray.Add(relateFileUrl); for (int i = 0; i < files.Count; i++) { var file = files[i]; var fileName = reportFile; if (string.IsNullOrEmpty(fileName)) { fileName = DateTime.Now.Ticks.ToString(); } fileName = fileName + Path.GetExtension(file.FileName); var filePath = PathUtils.Combine(EFileUploadTypeUtils.GetFileUploadPath(EFileUploadType.AppCommon.ToString(), _appFolders, app), fileName); var relateFileUrl = filePath.Replace(System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }), string.Empty); DirectoryUtils.CreateDirectoryIfNotExists(filePath); file.SaveAs(filePath); fileArray.Add(relateFileUrl); } return(Ok(new { BaseUrl = Request.RequestUri.Host, Files = fileArray })); } }