Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件上传 format 第一个参数 files 为空数组 #547

Closed
limodou opened this issue Jul 9, 2020 · 10 comments
Closed

文件上传 format 第一个参数 files 为空数组 #547

limodou opened this issue Jul 9, 2020 · 10 comments
Assignees
Milestone

Comments

@limodou
Copy link

limodou commented Jul 9, 2020

请先看《提问的智慧》,并尝试到黑客派搜寻资料解决问题。


编辑模式

请选择你遇到问题时使用的编辑模式(把正常情况模式删掉):

所见即所得模式

描述问题

在自定义后台上传返回的结果,使用 format 进行转换,但是第一个参数总是拿到空数组,原本我理解应该是原始的文件名数组。

期待的结果

期望返回原始文件名数组。

其他信息

responseText = vditor.options.upload.format(files as File [], xhr.responseText);

这里的 files 我得到是空数组

@Vanessa219
Copy link
Owner

你是通过拖拽、录音、点击上传按钮还是粘贴文本图片进行上传的呢?

@limodou
Copy link
Author

limodou commented Jul 9, 2020

点击上传。

@Vanessa219
Copy link
Owner

你这样打印了看一下输出

 format (files, responseText) {
      console.log(files, responseText)
    }

@limodou
Copy link
Author

limodou commented Jul 10, 2020

image
这里我们后台是没有返回前端的文件名的

@Vanessa219
Copy link
Owner

format 会将后端的 responseText 和文件一起传递给你,你转换为需要的 responseText 返回就可以了。
使用方式

new Vditor('vditor', {
upload: {
    format(files, responseText) {
      // 更改数据结构 JSON.parse(responseText);
      return JSON.stringify({
        // 要求的数据结构
      })
    },
    url: '/api/upload/editor',
}
})

源代码

if (vditor.options.upload.format) {
  responseText = vditor.options.upload.format(files as File [], xhr.responseText);
}

@Vanessa219 Vanessa219 added this to the 3.3 milestone Jul 10, 2020
@Vanessa219 Vanessa219 self-assigned this Jul 10, 2020
@limodou
Copy link
Author

limodou commented Jul 10, 2020

我知道这个,但是 files 是空数组。我粘贴图片 files 都是有内容的,比如:

image

@Vanessa219
Copy link
Owner

哦。我理解错了,不好意思

@umi-soft
Copy link

umi-soft commented Dec 7, 2020

我使用的【3.7.0】,同样也存在这个问题

@umi-soft
Copy link

umi-soft commented Dec 7, 2020

另外,我有一个疑问,官方文档里面有一段代码如下:
我看到咱们官方的文档中,相关代码如下:
image

上述代码中:依据请求状态【HTTPS STATUS】判定的,我们的后端服务,会全局封装一层正常或者异常的结果JSON,前端依据JSON内容识别正常或失败。
不知道format 返回之中的【code值】的取值都是啥样的?目前就知道值为0,正常,异常情况取值如何呢?
目前我format的逻辑大致是这样的:
image

@Vanessa219
Copy link
Owner

如果错误的话,把 code 设置为1,错误信息放在 msg 里面就可以了。
第一个为空的话,检查一下上传的文件是否被过滤了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants