问题

最近在将算法封装成UI进行展示,遇到如下
使用element的控件el-upload上传文件很慢,有时候会报错:

在这里插入图片描述

Interface 503 when uploading pictures。jsonplaceholder.typicode.com/posts/:1 POST https://jsonplaceholder.typicode.com/posts/ 503 ()

也有人在element下面提issue了,但是没有被解决:

原因

问题是由于el-upload控件样例如下:

<el-upload  class="avatar-uploader"  action="https://jsonplaceholder.typicode.com/posts/"  :show-file-list="false"  :on-success="handleAvatarSuccess"  :before-upload="beforeAvatarUpload">  <img v-if="imageUrl" :src="imageUrl" class="avatar">  <i v-else class="el-icon-plus avatar-uploader-icon"></i></el-upload>

其中使用的action是JSONPlaceholder ,这是一个虚拟的测试接口,测试post和get请求,但是这个网址反应慢,且会产生CORS跨域问题,导致Access-Control-Allow-Origin 异常,解决这个问题会比较复杂,对于只是新手内部测试的时候不是很明白如何操作,这里不做过多的介绍。

解决办法

在stackoverflow上面找到类似的问题,但是没有解答:
https://stackoverflow.com/questions/54150439/document-upload-through-fake-api-status-code-503-service-unavailable
看到它尝试更换fake API: https://www.mocky.io,对于他的情况没有什么改变,下面的回答是
trying to send the file to a third party API and looking for a way to test
我将el-upload的action地址从

action="https://jsonplaceholder.typicode.com/posts/"

跟换为

action="https://www.mocky.io/v2/5185415ba171ea3a00704eed/posts/"

成功解决问题,上传速度变快,且浏览器开发模式没有报相关的错误。

参考

收藏 打印