mockjs导致文件传输异常记录

2022-04-26 16:23:00
邢宗霖
原创
190

项目添加mockjs后使用axios进行文件上传下载错误处理:


    mock.js 版本:1.0.1-bata3


   文件上传时报错:

        报错信息:this.xhr.upload.addEventListener is not a funcation

       >>解决方案:

            在 node_modules/mockjs/dist/mock.js 第8308行 和 node_modules/mockjs/src/mock/xhr/xhr.js 第216行添加:
            // 创建原生xhr对象,调用原生open(),监听所有原生事件
            MockXMLHttpRequest.prototype.upload = xhr.upload;


    文件下载时文件信息错误:

        报错信息:文件内容乱码或格式错误

       >>解决方案:

                在 node_modules/mockjs/dist/mock.js 第8357行添加:


                // 原生 XHR
                // if (!this.match) {
                //     this.custom.xhr.send(data)
                //     return
                // }
                // 处理响应类型
                if (!this.match) {
                    this.custom.xhr.responseType = this.responseType
                    this.custom.xhr.send(data)
                    return
                }


发表评论
评论通过审核后显示。