Ant Design Vue如何上传文件、图片,复制即用(1)

上传的逻辑

import axios from “axios”;

//TS

interface FileItem {

uid: string;

name?: string;

status?: string;

response?: string;

url?: string;

}

//拦截提交事件,将文件数据填入List

const fileList = ref<FileItem[]>([]);

const beforeUpload = (file: FileItem) => {

fileList.value = […fileList.value, file];

formState.name = fileList.value[0].name;

return false;

};

//确认提交按钮回调,如果不要这个按钮可以直接在beforUpload里面调用

const onSubmit = (e) => {

const data = new FormData();

// 上传多个文件需要循环

fileList.value.forEach((file: FileItem) => {

file.status = “uploading”;

data.append(“file”, file.originFileObj);

data.append(“type”, “FILE”);

});

// 添加token

// data.append(“token”, String(token));

// 新建一个修改头部的axios,因为我其他的数据是通过URLSearchParams提交的,识别的头不同

const upload_ax = axios.create({

baseURL: “/api”, //在vite.config.js配置的跨域

withCredentials: true, //上传请求时是否携带 cookie

headers: {

“Content-Type”: “multipart/form-data”,

},

});

web浏览器中的javascript

window对象

  • 计时器

  • 浏览器定位和导航

  • 浏览历史

  • 浏览器和屏幕信息

  • 对话框

  • 错误处理

  • 作为window对象属性的文档元素

  • 17
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3.0中使用ant-design-vue文件,需要先安装ant-design-vue和axios模块: ```bash npm install ant-design-vue axios --save ``` 然后在需要使用上组件的页面中,引入ant-design-vue和axios模块: ```javascript import { Upload, Button } from 'ant-design-vue'; import axios from 'axios'; ``` 然后在页面中,添加上组件: ```html <template> <div> <upload :action="uploadUrl" :headers="headers" :showUploadList="false" :beforeUpload="beforeUpload" :onSuccess="onSuccess" :onError="onError" > <button> <a-button> <a-icon type="upload" /> Click to Upload </a-button> </button> </upload> </div> </template> ``` 其中,`uploadUrl`是上文件的接口地址,`headers`是上文件时需要携带的请求头,`beforeUpload`是上文件前的校验函数,`onSuccess`和`onError`分别是上成功和上失败的回调函数。 在页面的`script`中,需要定义这些属性的值以及`beforeUpload`、`onSuccess`、`onError`函数的实现: ```javascript export default { name: 'UploadPage', components: { Upload, Button }, data() { return { uploadUrl: '/api/upload', headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }; }, methods: { beforeUpload(file) { const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'; if (!isJpgOrPng) { this.$message.error('You can only upload JPG/PNG file!'); return false; } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error('Image must smaller than 2MB!'); return false; } return true; }, onSuccess(response, file) { this.$message.success('Upload successfully!'); }, onError(error, response, file) { this.$message.error('Upload error!'); } } }; ``` 其中,`beforeUpload`函数用来进行文件类型和大小的校验,`onSuccess`和`onError`函数用来处理上成功和上失败的情况。 最后,在页面的`style`中,可以添加一些样式来美化上按钮: ```css button { margin-top: 16px; } button .ant-btn { width: 100%; } button .anticon-upload { margin-right: 8px; } ``` 这样,就可以在Vue3.0中使用ant-design-vue文件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值