vue+axios+el-upload实现文件上传并跨域
时间: 2023-12-16 10:03:21 浏览: 221
毕设项目基于Mybatis+Vue+axios+原生JavaWeb的文件管理系统源码.zip
要实现Vue + Axios + El-upload进行文件上传并跨域,需要按照以下步骤进行操作:
1. 在Vue项目中安装axios和element-ui。在命令行中输入以下命令即可安装:
```
npm install axios element-ui --save
```
2. 在Vue项目中创建一个上传文件的组件,例如FileUpload.vue。在该组件中导入axios和element-ui,并且引入El-upload组件。示例代码如下:
```vue
<template>
<div>
<el-upload
class="upload-demo"
:action="uploadUrl"
:on-success="handleUploadSuccess"
:before-upload="beforeUpload"
:headers="headers"
:data="formData"
:file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
</div>
</template>
<script>
import axios from 'axios'
import { Message } from 'element-ui'
export default {
name: 'FileUpload',
data () {
return {
fileList: [],
uploadUrl: 'http://example.com/upload',
formData: {},
headers: {
'Content-Type': 'multipart/form-data'
}
}
},
methods: {
handleUploadSuccess (response, file, fileList) {
// 上传成功后的处理逻辑
console.log(response)
},
beforeUpload (file) {
// 文件上传前的处理逻辑
console.log(file)
}
}
}
</script>
```
3. 在组件中实现文件上传的逻辑。在上传文件之前,需要设置请求头和请求数据,并且需要处理跨域请求。可以在组件的methods中定义一个upload方法,用来发送上传请求。示例代码如下:
```vue
<script>
import axios from 'axios'
import { Message } from 'element-ui'
export default {
name: 'FileUpload',
data () {
return {
fileList: [],
uploadUrl: 'http://example.com/upload',
formData: {},
headers: {
'Content-Type': 'multipart/form-data'
}
}
},
methods: {
handleUploadSuccess (response, file, fileList) {
// 上传成功后的处理逻辑
console.log(response)
},
beforeUpload (file) {
// 文件上传前的处理逻辑
console.log(file)
},
upload () {
let config = {
headers: this.headers,
withCredentials: true // 跨域请求需要设置withCredentials为true
}
let data = new FormData()
data.append('file', this.fileList[0].raw)
// 向后端发送上传请求
axios.post(this.uploadUrl, data, config)
.then(response => {
this.handleUploadSuccess(response)
})
.catch(error => {
console.log(error)
})
}
}
}
</script>
```
4. 最后,在Vue组件中使用El-upload组件,并且调用upload方法即可实现文件上传。示例代码如下:
```vue
<template>
<div>
<el-upload
class="upload-demo"
:action="uploadUrl"
:on-success="handleUploadSuccess"
:before-upload="beforeUpload"
:headers="headers"
:data="formData"
:file-list="fileList">
<el-button size="small" type="primary" @click="upload">点击上传</el-button>
</el-upload>
</div>
</template>
<script>
import axios from 'axios'
import { Message } from 'element-ui'
export default {
name: 'FileUpload',
data () {
return {
fileList: [],
uploadUrl: 'http://example.com/upload',
formData: {},
headers: {
'Content-Type': 'multipart/form-data'
}
}
},
methods: {
handleUploadSuccess (response, file, fileList) {
// 上传成功后的处理逻辑
console.log(response)
},
beforeUpload (file) {
// 文件上传前的处理逻辑
console.log(file)
},
upload () {
let config = {
headers: this.headers,
withCredentials: true // 跨域请求需要设置withCredentials为true
}
let data = new FormData()
data.append('file', this.fileList[0].raw)
// 向后端发送上传请求
axios.post(this.uploadUrl, data, config)
.then(response => {
this.handleUploadSuccess(response)
})
.catch(error => {
console.log(error)
})
}
}
}
</script>
```
阅读全文