后端文件上传后会返回文件元数据,前端需要将文件路径转换为可访问的 URL。
{
fileId: "1234567890", // 文件唯一ID(必需,用于访问文件)
filePath: "tenant-logo/2025/12/10/a111492084ab4a5fb3f149f76b5f8c4c.png", // 文件存储路径(仅用于记录)
accessUrl: "http://example.com/uploads/...", // 可选:直接访问URL
originalName: "logo.png",
fileSize: 12345,
// ... 其他字段
}
重要: 前端必须使用 fileId 来访问文件,filePath 仅用于后端存储,无法直接访问。
import { getFileDownloadUrl, getFileUrl, getImageUrl } from '@/utils'
const fileData = {
fileId: '1234567890',
filePath: 'tenant-logo/2025/12/10/xxx.png',
accessUrl: 'http://example.com/uploads/xxx.png'
}
const url = getFileUrl(fileData)
// 优先级: accessUrl > filePath > fileId
const fileId = '1234567890'
const url = getFileUrl(fileId)
// 结果: /dev-api/api/file/download/1234567890
注意: 不要直接传入 filePath,因为后端没有静态资源映射,无法通过路径访问文件。
<template>
<!-- 方式一:使用 v-bind -->
<img :src="getFileUrl(row.systemLogo)" alt="Logo">
<!-- 方式二:使用计算属性 -->
<img :src="logoUrl" alt="Logo">
<!-- 方式三:使用 n-image 组件 -->
<n-image :src="getFileUrl(row.systemLogo)" />
</template>
<script setup>
import { computed } from 'vue'
import { getFileUrl } from '@/utils'
const props = defineProps({
row: Object
})
const logoUrl = computed(() => getFileUrl(props.row.systemLogo))
</script>
<template>
<a :href="getFileDownloadUrl(fileId)" download>下载文件</a>
</template>
<script setup>
import { getFileDownloadUrl } from '@/utils'
const fileId = '1234567890'
</script>
import { getImageUrl } from '@/utils'
// 基础用法
const url = getImageUrl(filePath)
// 指定尺寸
const thumbnailUrl = getImageUrl(filePath, {
width: 200,
height: 200,
mode: 'crop' // 'fit' | 'fill' | 'crop'
})
获取文件访问 URL
参数:
fileData - 文件路径字符串或文件元数据对象
"tenant-logo/2025/12/10/xxx.png" 或 fileId{ fileId, filePath, accessUrl }返回: string - 完整的文件访问 URL
优先级:
accessUrlfilePathfileId获取文件下载 URL
参数:
fileId - 文件ID返回: string - 文件下载 URL
获取图片预览 URL(带缩略图参数)
参数:
filePath - 文件路径options - 选项对象(可选)
width - 宽度height - 高度mode - 缩放模式: 'fit' | 'fill' | 'crop'返回: string - 图片预览 URL
文件 URL 会根据环境自动添加前缀:
开发环境 (VITE_REQUEST_PREFIX=/dev-api):
/dev-api/api/file/download/1234567890生产环境 (VITE_REQUEST_PREFIX=/cbc-server):
/cbc-server/api/file/download/1234567890fileId 和下载接口fileId 并用于访问fileId 而不是 filePathaccessUrl 是完整的 URL(包含 http/https),会直接使用,不会添加前缀<template>
<div class="file-display">
<!-- 显示图片 -->
<n-image
v-if="fileData.filePath"
:src="getFileUrl(fileData)"
width="200"
/>
<!-- 下载按钮 -->
<n-button
type="primary"
@click="handleDownload"
>
下载文件
</n-button>
</div>
</template>
<script setup>
import { getFileDownloadUrl, getFileUrl } from '@/utils'
const fileData = {
fileId: '1234567890',
filePath: 'tenant-logo/2025/12/10/a111492084ab4a5fb3f149f76b5f8c4c.png',
originalName: 'logo.png'
}
function handleDownload() {
const url = getFileDownloadUrl(fileData.fileId)
window.open(url, '_blank')
}
</script>