public async Task <IActionResult> UploadFiles(IFormFile file, string guid) { var filextention = Path.GetExtension(file.FileName); //if (filextention != ".xls" && filextention != ".xlsx") //验证文件格式 using (var stream = file.OpenReadStream()) { var objid = await _mongoRepository.UploadFileAsync(stream, file.FileName, new GridFSUploadOptions { Metadata = new BsonDocument { { "PostId", guid } } }); if (objid != null) { ApiDataHelper.GetHtmlId(objid.ToString()); return(Json(new Response <string> { Data = objid.ToString(), Msg = "上传成功", Status = 1 })); } return(Json(new Response <string> { Data = "", Msg = "上传失败", Status = 0 })); } }
/// <summary> /// get content by file id /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <IActionResult> GetKnownHtml(string id) { var fileInfo = await _mongoRepository.GetFileInfoAsync(id); if (FileHelper.FileCollections.ContainsKey(Path.GetExtension(fileInfo.Filename).ToLowerInvariant())) { switch (FileHelper.FileCollections[Path.GetExtension(fileInfo.Filename).ToLowerInvariant()]) { case 0: //无须转且可直接预览 //return await DownloadFile(id); return(new FileContentResult(await GetByte(id), FileHelper.GetContentType(fileInfo.Filename))); //return Content(await GetText(id), FileHelper.GetContentType(fileInfo.Filename)); case 1: //必须转 if (fileInfo.Metadata.Contains("HtmlId") && fileInfo.Metadata["HtmlId"] != "") { string text = await GetText(fileInfo.Metadata["HtmlId"].ToString(), "Htmls"); return(Content(text, "text/html", Encoding.UTF8)); } var htmlid = await ApiDataHelper.GetHtmlId(id); var hz = 0; while (htmlid == "" && hz < 2) { htmlid = await ApiDataHelper.GetHtmlId(id); hz++; } if (htmlid == "") //again { return(Content("文件转换多次未成功,请查看文档及转换接口", "text/html", Encoding.UTF8)); } var stext = await GetText(htmlid, "Htmls"); return(Content(stext, "text/html", Encoding.UTF8)); default: //todo break; } } return(Content("附件格式无法识别...", "text/html", Encoding.UTF8)); }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="postguid"></param> /// <param name="type">1:QA 2:Reply</param> /// <returns></returns> public async Task <IActionResult> QAUploadFiles(IFormFile file, string postguid, int type = 1) { if (file == null || file.Length == 0) { return(Json(new Response <bool> { Data = false, Msg = "未选择文件", Status = 0 })); } var filextention = Path.GetExtension(file.FileName); //if (filextention != ".xls" && filextention != ".xlsx") // return Json(new Response<bool> { Data = false, Msg = "请选择Excel文件", Status = 0 }); //验证文件格式 GridFSUploadOptions options = new GridFSUploadOptions { Metadata = new BsonDocument { { "PostId", postguid }, { "Type", type } } }; using (var stream = file.OpenReadStream()) { var id = await _mongoRepository.UploadFileAsync(stream, file.FileName, options); ApiDataHelper.GetHtmlId(id.ToString()); return(Json(new Response <object> { Status = 1, Msg = "", Data = new { id, Name = file.FileName } })); } }