el-upload文件图片上传;el-upload上传跨域问题

1.axios配置

if(config.isFormData){
 config.headers["Content-Type"] = "multipart/form-data"; 
}

2.el-upload

   <el-upload action="abcd"   :http-request="uploadFile">
    </el-upload>
      //自定义上传方法  用action有跨域的问题
      uploadFile(params) {
          const file = params.file;
          // 使用FormData传参数和文件
          var form = new FormData();
          // 文件
          form.append("file", file);
          // 调用封装好的上传方法,传给后台FormData
          upload(form).then(data => {
                console.log('上传图片成功')
          }).catch(response => {
                console.log('图片上传失败')
          })

    }

3.请求方法

export const upload = (formData) => { 
    return request({
          url: '/api/blade-resource/oss/endpoint/put-file',
          method: 'post',
          isFormData:true, 
          data:formData }
    )}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php /**  * Upload  *  * @author      Josh Lockhart <[email protected]>  * @copyright   2012 Josh Lockhart  * @link        http://www.joshlockhart.com  * @version     2.0.0  *  * MIT LICENSE  *  * Permission is hereby granted, free of charge, to any person obtaining  * a copy of this software and associated documentation files (the  * "Software"), to deal in the Software without restriction, including  * without limitation the rights to use, copy, modify, merge, publish,  * distribute, sublicense, and/or sell copies of the Software, and to  * permit persons to whom the Software is furnished to do so, subject to  * the following conditions:  *  * The above copyright notice and this permission notice shall be  * included in all copies or substantial portions of the Software.  *  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */ namespace Upload; /**  * FileInfo Interface  *  * @author  Josh Lockhart <[email protected]>  * @since   2.0.0  * @package Upload  */ interface FileInfoInterface {     public function getPathname();     public function getName();     public function setName($name);     public function getExtension();     public function setExtension($extension);     public function getNameWithExtension();     public function getMimetype();     public function getSize();     public function getMd5();     public function getDimensions();     public function isUploadedFile(); } File Upload 是一款非常强大的文件处理插件,支持多文件,拖拽上,进度条,文件验证及图片音视频预览,等等。可以说你能想到的功能它都有。你没想到的功能它也有。。不过由于功能太强大,使用起来还是需要点基本功,否则调试开发会遇到困难。
要实现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> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值