文件下载功能

文件下载功能

结构

 <label>附件:</label><span
                    class='pointer color-1890ff' @click='openFile(attachmentUrl, fileName)'>{{ fileName }}</span
                >
逻辑1-不抽离写法
<script lang='ts'>
import {downloadFile} from '@/axios';
import {defineComponent, reactive, toRefs} from 'vue';

export default defineComponent({
	name: 'index',
	components: {},
	setup(props, context) {
		const router = useRouter();
		const route = useRoute();
        const funMethods = {
            // 查看详情
			handleDetails(row) {
				router.push({
					path: 'screen',
					query: {
						resumeId: row.resumeId,
						desc: row.desc,
						id: row.id,
						flag: 'view'
					}
				});
			},
        };
        const openUrl = (path, name) => {
                // window.open(path)
                // downloadFile(path, name);
                if(path.indexOf(".pdf") != -1 || path.indexOf(".png") != -1 || path.indexOf(".jpg") != -1 || path.indexOf(".jpeg") != -1){
                    window.open(path); //  跳转链接
                }else{
                    if (!path) return ElMessage.error('文件地址不存在!');
                    downloadFile(path, name);     
                }
            };
        // 生命周期
		return {
			...toRefs(state),
			...funMethods,
			openUrl
		};
	}
});
</script>
逻辑2-抽离写法
<script lang='ts'>
import { defineComponent, toRefs } from 'vue';
import { ElMessage } from 'element-plus';
import { downloadFile } from '@/axios';
import useDownFileMethods from '@/views/PersonalCenter/MyProcess/hooks/useDownFileMethods'
export default defineComponent({
    name: 'leaveCom',
    props: [
        'state', 
        'name' 
    ],
    components: {  },
	
    setup(props) {
        //定义方法
        //const openFile =useDownFileMethods(props).openFile
        //或
	    const {
		    openFile
	    } =useDownFileMethods(props)
        return {
	        openFile,
            ...toRefs(props.state),
        };
    }
});
</script>

附:

const { obj } = this.props 写法含义

等价于, const obj = this.props.obj;
这是遵循ES6的语法的简写形式

公共方法

useDownFileMethods.ts

/*流程查看详情 公共方法*/

import {ElMessage} from "element-plus";
import {downloadFile} from "@/axios";
import {reactive} from "vue";

function useDownFileMethods(props) {

    const tableData = reactive([
        ...(props.state?.staffInvoiceInfoDtoList || [])
    ]);
    const openFile = (file, fileName) => {
        //if (!file) return ElMessage.warning('文件地址不存在!');
        //downloadFile(file, fileName);
        if(file.indexOf(".pdf") != -1 || file.indexOf(".png") != -1 || file.indexOf(".jpg") != -1 || file.indexOf(".jpeg") != -1){
            window.open(file); //  跳转链接  
        }else{
            if (!file) return ElMessage.error('文件地址不存在!');
            downloadFile(file, fileName);     
        }
    }
    const formatterTimesign = (timeSign) => {
        return {0: '全天', 1: '上午', 2: '下午'}[timeSign];
    }

    return {
        openFile,
        formatterTimesign,
        tableData
    }
}

export default useDownFileMethods

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);
         });
 };
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值