// GET: Admin/Upload public ActionResult UploadResource(string entityName, string entityId, bool optimize = true, bool apiEnabled = true, params string[] sizes) { var uniqueFolder = $"{entityName}-{entityId}"; var files = Request.Files; var serverRoute = HostingEnvironment.ApplicationPhysicalPath; if (files.Count <= 0) { return(Json(new { file = "" }, JsonRequestBehavior.AllowGet)); } var daFile = files[0]; var fileRequest = new SaveFileInput(System.Web.HttpContext.Current, daFile, serverRoute, Imgfolder, uniqueFolder) { ClearFolder = false, ApiEnabled = apiEnabled, OptimizationOptions = new OptimizationOptions() { Optimize = optimize, Sizes = sizes } }; var fileLocation = FileSaver.SaveFile(fileRequest); //new ProcessImg().Process(fileLocation.File); return(Json(fileLocation, JsonRequestBehavior.AllowGet)); }
public async Task <SavedFileResponse> SaveFile(SaveFileInput input) { var result = await _fileStoreManager.SaveFile(new FileManagerServiceInput() { CreateUniqueName = false, File = input.File, SpecialFolder = input.SaveFolder, VirtualFolder = "/Content/Attachments/", Properties = input.Properties }, CinotamFileManagerService.UseCdn); return(new SavedFileResponse() { FileName = input.File.FileName, Url = result.Url, StoredInCloud = result.WasStoredInCloud }); }
public ActionResult SaveFile() { string body; using (var reader = new StreamReader(Request.InputStream)) body = reader.ReadToEnd(); SaveFileInput input = new SaveFileInput { Data = body, ServerOrigin = "http://" + Request.ServerVariables.Get("HTTP_HOST").ToString() }; if (fileManageService.SaveFile(input)) { return(Json(new { error = "0" })); } return(Json(new { error = "1" })); }
public bool SaveFile(SaveFileInput input) { try { var fileData = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(input.Data); int status = (int)fileData["status"]; //状态4处理手动保存后关闭文档的响应,让远程可以回调 if (status == 4) { var key = fileData["key"].ToString(); DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); //处理远程回调 if (documentInfo.ExternalFlag) { ExternalCallback(documentInfo, input.ServerOrigin, false); } } else if (status == 2) { var key = fileData["key"].ToString(); var userList = (ArrayList)fileData["users"]; string userId = userList.Count > 0 ? userList[0].ToString() : null; var user = _context.Users.FirstOrDefault(a => a.UserID == userId); DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); DocumentHistory documentHistory = _context.DocumentHistories.FirstOrDefault(a => a.DocumentInfoId == documentInfo.Id && a.Version == documentInfo.CurrentVersion); documentInfo.Key = Guid.NewGuid().ToString(); documentInfo.ModifyTime = DateTime.Now; documentInfo.ModifyUserId = user?.UserID; documentInfo.ModifyUserName = user?.UserName; //不使用history的key,因为可能修改文件后没有产生新版本 documentHistory.Key = documentInfo.Key; documentService.StoreDocumentFile(documentInfo, (string)fileData["url"]); //txt文件需要做转换处理 string convertPath = ConvertFile(documentInfo); if (!string.IsNullOrWhiteSpace(convertPath)) { documentService.StoreDocumentFile(documentInfo, convertPath); } _context.SaveChanges(); //处理远程回调 if (documentInfo.ExternalFlag) { ExternalCallback(documentInfo, input.ServerOrigin, true); } } else if (status == 6) { var key = fileData["key"].ToString(); var historyDic = (Dictionary <string, object>)fileData["history"]; var serverVersion = historyDic["serverVersion"].ToString(); var changes = new JavaScriptSerializer().Serialize(historyDic["changes"]); var changesUrl = fileData["changesurl"].ToString(); var userList = (ArrayList)fileData["users"]; string userId = userList.Count > 0 ? userList[0].ToString() : null; //更新文档信息 DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); DocumentHistory documentHistory = _context.DocumentHistories.Where(a => a.DocumentInfoId == documentInfo.Id) .OrderByDescending(a => a.Version).FirstOrDefault(); bool newVersion = documentHistory.Key == documentInfo.Key; documentInfo.CurrentVersion = newVersion ? documentHistory.Version + 1 : documentHistory.Version; _context.SaveChanges(); //存储新文档 var user = _context.Users.FirstOrDefault(a => a.UserID == userId); string userName = user?.UserName; CreateHistoryInput createHistoryInput = new CreateHistoryInput { DocumentInfoId = documentInfo.Id, Version = documentInfo.CurrentVersion, PreviousVersion = documentInfo.CurrentVersion - 1, ServerVersion = serverVersion, Key = newVersion ? Guid.NewGuid().ToString() : documentHistory.Key, Changes = changes, UserId = userId, UserName = userName, AutoSave = false, CreateTime = DateTime.Now, FileType = documentInfo.FileType, HistoryUrl = (string)fileData["url"], ChangesUrl = changesUrl }; documentService.CreateHistory(createHistoryInput); } return(true); } catch { return(true); } }