这个问题的主要原因是onlyoffice服务端无法调用我们传给onlyoffice服务器的回调地址或是我们传的回调接口返回的值是onlyoffice服务端无法识别的。如文档中要求接口返回{\"error\":0}表示回调成功。结果我们的接口没有返回或返回的值不是{\"error\":0},这时onlyoffice服务端就是认为接口无法保存文档,这时这个提示就出来了。
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);
//生成文件,也可以使用Mongodb来保存文件
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);
}}