| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { Loading } from 'element-ui';
- /**
- * 从 HTTP 头的 Content-Disposition 字段中提取文件名
- * @param {string} contentDisposition - HTTP 头的 Content-Disposition 字段值
- * @returns {string|null} 提取的文件名,若未找到则返回 null
- */
- export function ExtractFilename(contentDisposition) {
- if (!contentDisposition) return null;
- // 匹配 filename= 后面的内容(直到分号或字符串结束)
- const match = contentDisposition.match(/filename=([^;]+)/i);
- if (!match) return null;
- let filename = match[1].trim();
- // 去除可能的引号
- if (filename.startsWith('"') && filename.endsWith('"')) {
- filename = filename.slice(1, -1);
- }
- try {
- // URL 解码
- return decodeURIComponent(filename);
- } catch (e) {
- console.warn('URL解码失败,返回原始文件名:', e);
- return filename;
- }
- }
- /**
- * 触发浏览器下载文件
- * @param {Blob} blob - 要下载的文件的 Blob 对象
- * @param {string} fileName - 下载时使用的文件名
- */
- export function DownloadFile(blob, fileName) {
- const url = window.URL.createObjectURL(blob);
- const link = document.createElement('a');
- link.href = url;
- link.download = fileName;
- link.click();
- window.URL.revokeObjectURL(url);
- link.remove();
- }
- /**
- * 创建一个加载中实例
- * @param {Object} options - 加载中配置选项
- * @returns {Object} 加载中实例
- */
- export function CreateLoadingInstance(options) {
- options = Object.assign({
- lock: true,
- text: '正在处理...',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- }, options || {});
- return Loading.service(options);
- }
|