- 基于上面的问题,须要使用form表单上传文件
- form表单上传会引发页面的刷新,所以须要动态添加一个iframe来避免页面刷新
- from表单上传以后须要调用回调,此时须要监听iframe的onload事件
- 文件上传以后的返回值
Content-Type
值不能是application/json
这会致使IE去解析返回结果,最终调用文件的保存或者打开,此处须要与后端协商将Content-Type
改成text/plain
- 若是须要图片回显,回显的图片路径中有有
query
参数,若是有多个参数会出现&
,可是返回结果显示在iframe中因此&会被当作HTML解析为&
因此回显以前须要将此处转换回来
改用vue-upload-component 做者对IE9专门作了兼容,就是使用起来理解成本有点儿高javascript
如何触发上传vue
经过ref获取upload实例,在添加文件时 激活上传java
this.$refs.upload.active = true
如何判断当前上传的状态(添加,更新,删除,上传成功,上传失败)jquery
每次上传的状态变化时 都会调用
@input-file
绑定的回调,形参是newFile, oldFile,经过新旧文件的对比来获得当前的状态,感受有点儿反策略模式的意思,本身经过元状态的组合来获得当前状态,习惯的话以为仍是挺有意思的
inputFile(newFile, oldFile) { // 旧文件活跃 新文件不活跃 此时上传过程完成 if (newFile && oldFile && !newFile.active && oldFile.active) { this.$refs.upload.active = false // 得到相应数据 let res = '{}' // 此处判断相对简单,能够参考jquery.form.js中作的判断 if (/<pre/.test(newFile.response)) { res = />(.*)</.exec(newFile.response)[1] } res = JSON.parse(res) if (res.code !== 200) { if (res.code === 402) { this.$route.push('/login') return } Message.error(res.message) } else { Message.success('上传成功') // 回显图片 this.upload.url = res.data.url.replace(/&/g, '&') } if (newFile.xhr) { // 得到响应状态码 console.log('status', newFile.xhr.status) } } // 添加文件 if (newFile && !oldFile) { this.$refs['upload' + this.index].active = true } }