public class OfficeFilesController : BaseController//Controller
{
/// <summary> /// 保存OnlyOffice文件 /// </summary> [HttpPost] public void SaveOnlyOfficeFile() { var fileDataJson = string.Empty; var method = "SaveOnlyOfficeFile"; bool isSucceed = false; string search1 = string.Empty; string search2 = string.Empty; string status = string.Empty; var ip = CommonHelpLD.ServiceIp; var model = new OnlyOfficeFile(); string body = string.Empty; try { using (var reader = new StreamReader(HttpContext.Request.Body)) { body = reader.ReadToEndAsync().Result; } if (!string.IsNullOrEmpty(body)) { var endIndex = body.IndexOf("fileDataJson");//, fileDataJson: if (endIndex > 0) { body = body.Substring(0, endIndex - 15); body = body.Trim().TrimEnd(','); } var fileData = JsonConvert.DeserializeObject<CallbackApiReqModel>(body); if (fileData != null && (fileData.status == 2 || fileData.status == 6)) { status = fileData.status.ToString(); try { if (fileData.key.Contains("_")) { var keys = fileData.key.Split('_'); if (keys.Length >= 3) { fileData.Id = string.Format("{0}_{1}", keys[0], keys[1]); } else { fileData.Id = keys[1]; } } else { fileData.Id = fileData.key; } var fileModel = OnlyOfficeFileHelper.GetFileObjModel(fileData.key);//(fileData.Id); var sFilePath = OnlyOfficeFileHelper.GetFilePath(fileModel.FileType); string sFileName = string.Format("{0}.docx", fileModel.Key); var PATH_FOR_SAVE = sFilePath + "/" + sFileName; //文件的绝对路径 search1 = fileModel.Key; search2 = fileData.key; model.FileKey = fileData.key; model.Type = fileModel.FileType; model.Name = sFileName; model.DisplayName = sFileName; model.TaskId = search1; model.CreatedBy = "CallbackApi"; model.VersionNo = 1; var model_o = _serviceOnlyOfficeFile.GetModelByTaskId(model.TaskId, model.Type); if (model_o != null) { model.VersionNo = model_o.VersionNo + 1; } var req = WebRequest.Create(fileData.url); using (var stream = req.GetResponse().GetResponseStream()) { using (var fs = System.IO.File.Open(PATH_FOR_SAVE, FileMode.Create)) { var buffer = new byte[4096]; int readed; while ((readed = stream.Read(buffer, 0, 4096)) != 0) { fs.Write(buffer, 0, readed); } } } isSucceed = true; model.FilePath = PATH_FOR_SAVE.Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty); } catch (Exception ex) { isSucceed = false; } } else { isSucceed = true; } fileDataJson = JsonConvert.SerializeObject(fileData); } else { isSucceed = true; } } catch (Exception ex) { isSucceed = false; } var rspJosn = "{\"error\":0}";//返回保存成功 if (isSucceed) { _serviceOnlyOfficeFile.SaveModel(model); } else { rspJosn = "{\"error\":1}";//返回保存失败 } _serviceCommonLog.AddLog(_className, method, "回调接口", body, rspJosn, "status:" + status, ip, search1, search2); Response.WriteAsync(rspJosn); }
}
标签:body,core,string,fileData,var,net,OnlyOffice,model,isSucceed From: https://www.cnblogs.com/stevenchen2016/p/17524781.html