【vue】AntDV组件库中a-upload实现文件上传:


一、文档:

Upload 上传–Ant Design Vue

image.png

二、使用(以Jeecg为例):
【1】template:
<a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader"
	 :data="{ 'orderId': record.id }" :action="importExcelUrlDispatchCar"
   @change="handleImportExcel">导入
</a-upload>
【2】script:
<script>
 	export default {
  	data() {
    	return {
        
        url:{
           importExcelUrlDispatchCar:"/kd/xxx/importTemplate"
        }
      }
    },
    computed: {
        //拼接上传的地址
        importExcelUrlDispatchCar: function () {
          return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlDispatchCar}`;
        },
      	// 设置上传的Header参数
      	tokenHeader() {
         	let head = { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) }
          let tenantid = Vue.ls.get(TENANT_ID)
          if (tenantid) {
            head['tenant-id'] = tenantid
          }
      		return head;
      	},
    },
    methods:{
      handleImportExcel(info){
        this.loading = true;
      	if (info.file.status !==  'uploading') {
           console.log(info.file,  info.fileList);
        }
      	if (info.file.status === 'done') {
          this.loading = false;
          if (info.file.response.success){
          	if (info.file.response.code === 201) {
            	let { message, result:{ msg, fileUrl, fileName } }= info.file.response
            	let href = window._CONFIG['domianURL'] + fileUrl
            	this.$warning({
              	title: message,
             		content: (<div><span>{msg}</span>

                	<span>具体详情请 
                	<a href={href}target="_blank" download={fileName}>点击下载</a> 
                	</span></div>)
            	})
           	} else {
             	this.$message.success(info.file.response.message ||`${info.file.name} 文件上传成功`)
           	}
          	this.loadData()
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        }else if (info.file.status === 'error') {
          this.loading = false;
          if (info.file.response.status === 500) {
          	let data = info.file.response
            const token = Vue.ls.get(ACCESS_TOKEN)
            if (token && data.message.includes("Token失效")) {
              this.$error({
                title: '登录已过期',
                content: '很抱歉,登录已过期,请重新登录',
                okText: '重新登录',
                mask: false,
                onOk: () => {
                  store.dispatch('Logout').then(() => {
                    Vue.ls.remove(ACCESS_TOKEN)
                    window.location.reload();
                  })
                }
              })
            }
          }
        }else{
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      },
    }
	}
</script>
三、效果图:

image.png
image.png
image.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用 arco design 的 a-upload 组件实现图片上传功能,你需要先安装 arco design 和 axios(用于发送上传请求)两个依赖。 1. 安装 arco design 和 axios ```bash npm install arco-design axios --save ``` 2. 导入 a-upload 组件和 axios ```vue <template> <div> <a-upload :action="uploadUrl" :beforeUpload="beforeUpload" :onSuccess="onSuccess" :onError="onError" :onProgress="onProgress" > <a-button> <a-icon type="upload" /> 选择文件 </a-button> </a-upload> </div> </template> <script> import { AUpload } from 'arco-design-vue'; import axios from 'axios'; export default { components: { AUpload, }, data() { return { uploadUrl: 'http://your-upload-api-url', // 上传接口地址 }; }, methods: { beforeUpload(file) { // 在上传文件之前执行的操作,可以在这里对文件进行校验或者预处理 }, onSuccess(response, file) { // 上传成功后执行的操作,response 是服务器响应数据,file 是上传的文件对象 }, onError(err, response, file) { // 上传失败后执行的操作,err 是错误对象,response 是服务器响应数据,file 是上传的文件对象 }, onProgress(event, file, fileList) { // 上传过程中执行的操作,event 是上传进度事件对象,file 是上传的文件对象,fileList 是上传文件列表 }, }, }; </script> ``` 3. 通过设置 a-upload 的属性实现图片上传功能 在模板中,你需要使用 a-upload 组件实现文件上传功能。a-upload 组件提供了多个属性,包括: - `action`:上传接口地址,必填项。 - `beforeUpload`:上传文件之前执行的操作,可以在这里对文件进行校验或者预处理。 - `onSuccess`:上传成功后执行的操作,`response` 是服务器响应数据,`file` 是上传的文件对象。 - `onError`:上传失败后执行的操作,`err` 是错误对象,`response` 是服务器响应数据,`file` 是上传的文件对象。 - `onProgress`:上传过程中执行的操作,`event` 是上传进度事件对象,`file` 是上传的文件对象,`fileList` 是上传文件列表。 例如,你可以在 `beforeUpload` 方法中对上传的文件进行校验,代码如下: ```vue <template> <div> <a-upload :action="uploadUrl" :beforeUpload="beforeUpload" :onSuccess="onSuccess" :onError="onError" :onProgress="onProgress" > <a-button> <a-icon type="upload" /> 选择文件 </a-button> </a-upload> </div> </template> <script> import { AUpload } from 'arco-design-vue'; import axios from 'axios'; export default { components: { AUpload, }, data() { return { uploadUrl: 'http://your-upload-api-url', // 上传接口地址 }; }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg'; const isPNG = file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG) { this.$message.error('只能上传 JPG 或 PNG 格式的图片'); return false; } if (!isLt2M) { this.$message.error('上传的图片大小不能超过 2MB'); return false; } return true; }, onSuccess(response, file) { this.$message.success('上传成功'); }, onError(err, response, file) { this.$message.error('上传失败,请重试'); }, onProgress(event, file, fileList) { console.log(event, file, fileList); }, }, }; </script> ``` 在 `beforeUpload` 方法中,我们对上传的文件进行了类型和大小的校验,如果不符合要求就提示用户并返回 `false`,否则返回 `true`,继续执行上传操作。 在 `onSuccess` 方法中,我们做了一个简单的提示,告诉用户上传成功了。 在 `onError` 方法中,我们也做了一个简单的提示,告诉用户上传失败了。 在 `onProgress` 方法中,我们将上传进度信息打印到了控制台上,你可以根据实际情况做出对应的操作。 最后,你需要根据实际需求,修改上传接口地址和其他属性,就可以愉快地使用 a-upload 组件实现图片上传功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sun Peng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值