public async Task <FileResultModel> DownloadFileAsync(string filename) { if (filename == null) { return(null); } var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", filename); var memory = new MemoryStream(); using (var stream = new FileStream(path, FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; var fileName = Path.GetFileName(path) + '_' + DateTime.Now.ToShortDateString() + '_' + DateTime.Now.ToShortTimeString(); var fileResult = new FileResultModel { File = memory, Name = fileName, MimeType = GetContentType(path) }; return(fileResult); }
[RequestSizeLimit(100 * 1024 * 1024)] //限制http大小 public async Task <IActionResult> Post([FromHeader] string fileOwner, List <IFormFile> files) { try { if (string.IsNullOrWhiteSpace(fileOwner)) { return(AssertNotFound(new ResponseFileResult { Result = false, Code = ResponseCode.InvalidParameters, ErrorMessage = "文件归属不能为空" })); } if (files == null || !files.Any()) { return(AssertNotFound(new ResponseFileResult { Result = false, Code = ResponseCode.InvalidParameters, ErrorMessage = "附件不能为空" })); } string filePath = Path.Combine(Directory.GetCurrentDirectory(), BASEFILE, $@"{fileOwner}"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } var result = new ResponseFileResult(); var fileList = new List <FileResultModel>(); foreach (var file in files) { var fileModel = new FileResultModel(); var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); var newName = Guid.NewGuid().ToString() + Path.GetExtension(fileName); var filefullPath = Path.Combine(filePath, $@"{newName}"); using (FileStream fs = new FileStream(filefullPath, FileMode.Create))//System.IO.File.Create(filefullPath) { await file.CopyToAsync(fs); fs.Flush(); } fileList.Add(new FileResultModel { Name = fileName, Size = file.Length, Url = $@"/file/download?fileOwner={fileOwner}&fileName={newName}" }); } result.FileResultList = fileList; return(AssertNotFound(result)); } catch (Exception ex) { return(AssertNotFound(new ResponseFileResult { Result = false, Code = ResponseCode.UnknownException, ErrorMessage = ex.Message })); } }
public async Task <FileResultModel> UploadFile(Guid folderId) { var streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder); await Request.Content.ReadAsMultipartAsync(streamProvider); var fileResult = new FileResultModel { FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName), Names = streamProvider.FileData.Select(entry => entry.Headers.ContentDisposition.FileName), ContentTypes = streamProvider.FileData.Select(entry => entry.Headers.ContentType.MediaType), Description = streamProvider.FormData["description"], CreatedTimestamp = DateTime.UtcNow, UpdatedTimestamp = DateTime.UtcNow, DownloadLink = "TODO, will implement when file is persisited", FileSizes = streamProvider.FileData.Select(entry => entry.Headers.ContentDisposition.Size) }; this.documentService.InsertDocuments(folderId, fileResult); return(fileResult); // ---------------------------------------------- //int iUploadedCnt = 0; //// DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES. //string sPath = ""; //sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/locker/"); //System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; //// CHECK THE FILE COUNT. //for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++) //{ // System.Web.HttpPostedFile hpf = hfc[iCnt]; // if (hpf.ContentLength > 0) // { // // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE) // if (!File.Exists(sPath + Path.GetFileName(hpf.FileName))) // { // // SAVE THE FILES IN THE FOLDER. // hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName)); // iUploadedCnt = iUploadedCnt + 1; // } // } //} //// RETURN A MESSAGE. //if (iUploadedCnt > 0) //{ // return iUploadedCnt + " Files Uploaded Successfully"; //} //else //{ // return "Upload Failed"; //} }
public IEnumerable <DocumentModel> InsertDocuments(Guid folderId, FileResultModel fileResult) { Guard.ArgumentNotNull(fileResult, nameof(fileResult)); var documents = new List <DocumentModel>(); // TODO: Use Parallel library. if (EnumerableExtensions.Any(fileResult.Names)) { var names = fileResult.Names.Select(this.RemoveSpecialCharactersFromFileName).ToArray(); var fileNames = fileResult.FileNames.ToArray(); var contentTypes = fileResult.ContentTypes.ToArray(); for (var i = 0; i < names.Length; i++) { string documentText = System.IO.File.ReadAllText(fileNames[i]); documents.Add(new DocumentModel { Id = Guid.NewGuid(), Folder = new FolderModel { Id = folderId }, FolderId = folderId, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, FileName = names[i], TextNasPath = fileNames[i], FileType = contentTypes[i].FromTextAttributeStringToEnumValue <FileType>(), DocumentText = documentText }); } } var documentEntities = Mapper.Map <IEnumerable <DocumentEntity> >(documents); this.documentRepository.InsertDocuments(documentEntities); if (documentEntities.Any()) { foreach (var document in documentEntities) { this.elasticRepository.Create( document, ConfigurationManager.AppSettings["elasticSearchIndexName"]); } } return(documents); }
public DocumentModel ParseFile(FileResultModel fileResult) { Guard.ArgumentNotNull(fileResult, nameof(fileResult)); string text = System.IO.File.ReadAllText(fileResult.FileNames.FirstOrDefault()); throw new NotImplementedException(); //if (fileType == FileType.TXT.ToStringValue()) //{ // // TODO: Parse as .txt file. //} //if (fileType == FileType.DOC.ToStringValue()) //{ // // TODO: Parse as .doc file. //} //if (fileType == FileType.DOCX.ToStringValue()) //{ // // TODO: Parse as .docx file. //} //if (fileType == FileType.PDF.ToStringValue()) //{ // // TODO: Parse as .pdf file. //} //if (fileType == FileType.ODT.ToStringValue()) //{ // // TODO: Parse as .odt file. //} //if (fileType == FileType.RTF.ToStringValue()) //{ // // TODO: Parse as .rtf file. //} //if (fileType == FileType.TEX.ToStringValue()) //{ // // TODO: Parse as .tex file. //} }
public FileResultModel UploadAudio(FileUploadRequestModel param) { FileResultModel result = new FileResultModel(); try { int?memberId = IdentityHelper.GetMemberId(); if (!memberId.HasValue) { throw new OrgException("Invalid MemberId"); } UploadBL bl = new UploadBL(); var file = bl.AddFile(memberId.Value, param, OrgComm.Data.Models.Upload.UploadType.Audio); result.Status = true; result.File = file; result.Message = "Uploaded successfully"; } catch (OrgException oex) { result.Status = false; result.Message = oex.Message; } catch (Exception ex) { result.Status = false; result.Message = AppConfigs.InternalErrorMessage; if (AppConfigs.DebugInternalMessage) { result.InternalMessage = ex.Message; } } return(result); }