.netcore 保存onlyoffice文件

    /// <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);
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

acycwf

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

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

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

打赏作者

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

抵扣说明:

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

余额充值