iframe标签的使用(二)02——vue3.0页面结构的应用 & 文件下载功能-直接定义下载方法

iframe标签的使用(二)02——vue3.0页面结构的应用 & 文件下载功能-直接定义下载方法

效果-下载

在这里插入图片描述

结构
 <el-button type="primary" @click='openFile(attachmentUrl, fileName)'>下载</el-button> 

实例

<el-dialog v-model="dialogFormVisible" title="题目" width="660px" >
    <label>附件:</label>
    <span class="color-666666 color-1890ff hand" @click="openFile(url, fileName)">
        {{ fileName }}
    </span>
</el-dialog>
方法
方式2:直接定义方法
import { defineComponent, reactive, ref, toRefs } from 'vue';
import { downloadFile } from '@/axios';
import { useRoute } from 'vue-router';

export default defineComponent({
    name: 'name',
    props: [
        'state',
    ],
    setup(props, { emit }) {
        let dialogFormVisible = ref(false);
        const route = useRoute();
        const funMethods = {
            cancel() {
                emit('close');
            },
            openFile(openFile, fileName) {
                if (!openFile) return ElMessage.error('文件地址不存在!');
                downloadFile(openFile, fileName);
            }
        }; 
        return {
            dialogFormVisible,
            ...toRefs(props.state),
            ...funMethods
        };
    },
});
封装的下载方法-downloadFile()

src/axios/index.ts

 /**
  * 下载文件通过DFS url
  * @param url
  * @param name
  * @param params
  */
 export const downloadFile = (url: string, name?: string, params?: Object) => {
     axios
         .get(url, {
             params,
             responseType: 'blob'
         })
         .then(res => {
             let fileName = '';
             //判断是否存在 . 如果不存在说明是自己传入
             if (name.indexOf('.') === -1) {
                 fileName = name;
                 fileName += url.length
                     ? '.' + url.substring(url.lastIndexOf('.') + 1, url.length)
                     : '';
             } else {
                 //存在 filename 直接使用
                 fileName = name;
             }
             downloadByBlob(res.data, fileName);
         });
 };

 export const downloadByBlob = (blob: Blob, fileName: string) => {
     //不建议改这个 post 还有get  流下载都通过这种形式
     const b = new Blob([blob],{type:'application/vnd.ms-excel'});
     let link = document.createElement('a');
     const href = URL.createObjectURL(b);
     link.href = href;
     link.download = fileName;
     link.click();
     link = null;
     window.URL.revokeObjectURL(href);
 };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值