OnlyOffice文件回调接口net core 版本,提示:这份文件无法保存。请检查连接设置或联系你的管理员。当你点击ok按钮,系统将提示你下载文档。

这个问题的主要原因是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);
        }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
首先,你需要在 ASP.NET Core 项目中安装 MySQL 的 ADO.NET 提供程序,方法是在项目的 `csproj` 文件中添加以下行: ``` <ItemGroup> <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" /> </ItemGroup> ``` 然后,你需要在项目的 `Startup.cs` 文件的 `ConfigureServices` 方法中添加 MySql 的服务配置: ```csharp services.AddDbContext<MyDbContext>(options => options.UseMySQL(Configuration.GetConnectionString("MySQLConnection"))); ``` 在这里,`MyDbContext` 是你的数据库上下文类,它继承自 `Microsoft.EntityFrameworkCore.DbContext`。`Configuration.GetConnectionString("MySQLConnection")` 表示读取项目配置文件(例如 appsettings.json)中名为 "MySQLConnection" 的连接字符串。 然后,你需要在项目中创建一个控制器类,用于处理文件下载求。例如,你可以创建一个名为 `FileController` 的控制器,其中包含一个名为 `Download` 的 action 方法,用于处理文件下载求: ```csharp using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.IO; using System.Linq; using System.Threading.Tasks; namespace MyProject.Controllers { [Route("api/[controller]")] [ApiController] public class FileController : ControllerBase { private readonly MyDbContext _dbContext; public FileController(MyDbContext dbContext) { _dbContext = dbContext; } [HttpGet] [Route("download")] public async Task<IActionResult> Download(int fileId) { // 查询数据库中的文件记录 var file = await _dbContext.Files .Where(f => f.Id == fileId)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StevenChen85

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值