/// <summary>
/// 保存OnlyOffice文件
/// </summary>
[HttpPost]
public async void SaveOnlyOfficeFile()
{
var fileDataJson = string.Empty;
bool isSucceed = false;
string body = string.Empty, url = 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<JObject>(body);
//*0 - 找不到带有密钥标识符的文档,
//*1 - 正在编辑文档,
//*2 - 文件已准备好保存,
//*3 - 发生了文档保存错误,
//*4 - 文件关闭,没有变化,
//*6 - 正在编辑文档,但保存当前文档状态,
//*7 - 强制保存文档时发生错误。
int status = (int)fileData["status"];
if ((status == 2 || status == 6) && fileData.ContainsKey("key") && fileData.ContainsKey("url"))
{
//获取key
var fileno = (string)fileData["key"];
//解析文件路径
var docInfo = await _fileInfoService.GetSingle(a => a.State < 99 && a.FileNo == fileno);
if (docInfo != null)
{
var filepath = docInfo.FileUrl.Substring(docInfo.FileUrl.IndexOf("/Upload"));
var dirTempPath = $"{Directory.GetCurrentDirectory()}{filepath}";//文件本地路径
//文件下载地址
url = (string)fileData["url"];
//保存文件
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
using (FileStream fs = System.IO.File.Create(dirTempPath))
{
stream.CopyTo(fs);
fs.Flush();
}
}
}
}
}
}
}
isSucceed = true;
}
else
{
isSucceed = true;
}
}
catch (Exception ex)
{
isSucceed = false;
}
var rspJosn = "{\"error\":0}";//返回保存成功
if (!isSucceed)
{
rspJosn = "{\"error\":1}";//返回保存失败
}
await Response.WriteAsync(rspJosn);
}