当前位置: 首页 > news >正文

linux增加网站链接地址

linux增加网站,链接地址,哈尔滨网站建设30t,旅游行业网站建设文章目录 Vue 项目中下载返回的文件流操作步骤一、使用 Axios 请求文件流数据二、设置响应类型为 ‘blob’三、创建下载链接并触发下载四、在 Vue 组件中集成下载功能五、解释与实例说明1、使用 Axios 请求文件流数据:设置响应类型为 blob:创建下载链接并…

文章目录

    • Vue 项目中下载返回的文件流操作步骤
    • 一、使用 Axios 请求文件流数据
    • 二、设置响应类型为 ‘blob’
    • 三、创建下载链接并触发下载
    • 四、在 Vue 组件中集成下载功能
    • 五、解释与实例说明
      • 1、使用 Axios 请求文件流数据:
      • 设置响应类型为 'blob':
      • 创建下载链接并触发下载:
      • 下载文件,文件名获取不到

Vue 项目中下载返回的文件流操作步骤

1、使用 Axios 请求文件流数据;

2、设置响应类型为 ‘blob’;

3、创建下载链接并触发下载。

其中,最关键的一步是创建一个 Blob 对象并使用 URL.createObjectURL 方法生成一个下载链接。以下将详细说明如何实现。

一、使用 Axios 请求文件流数据

在 Vue 项目中,需要使用 Axios 来发送请求并获取文件流数据。可以在组件中使用 Axios 进行 HTTP 请求,确保在请求头中设置适当的响应类型。

import axios from 'axios';export default {methods: {downloadFile() {axios({method: 'get',url: 'your-file-endpoint',  // 替换为实际的文件流接口params:{},responseType: 'blob'}).then(response => {this.handleFileDownload(response.data);}).catch(error => {console.error('Error downloading the file', error);});}}
};

二、设置响应类型为 ‘blob’

在 Axios 请求中,设置 responseType 为 ‘blob’,这样 Axios 会将响应数据处理为 Blob 对象。Blob 对象表示一个不可变的、原始数据的类文件对象。

三、创建下载链接并触发下载

为了触发文件下载,需要创建一个 URL 链接并模拟点击事件。可以在组件中添加一个方法来处理 Blob 数据并进行下载。

methods: {/*** 拿到文件流后的下载的方法* @param {*} data* @param {*} filename* @param {*} mime* @param {*} bom*/function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });if (typeof window.navigator.msSaveBlob !== 'undefined') {window.navigator.msSaveBlob(blob, filename);} else {const blobURL = window.URL.createObjectURL(blob);const tempLink = document.createElement('a');tempLink.style.display = 'none';tempLink.href = blobURL;tempLink.setAttribute('download', filename);if (typeof tempLink.download === 'undefined') {tempLink.setAttribute('target', '_blank');}document.body.appendChild(tempLink);tempLink.click();// 移除 a 标签document.body.removeChild(tempLink);   // 释放 URL 对象window.URL.revokeObjectURL(blobURL);}}
}

四、在 Vue 组件中集成下载功能


<template><div><button @click="downloadFile">Download File</button></div>
</template>
<script lang="ts" setup>// 下载
async function downloadFile() {try {axios({method: 'get',url: 'your-file-endpoint',  // 替换为实际的文件流接口params: {},   // 请求参数responseType: 'blob'}).then((response) => {console.log('downloadByData',response)if(response.headers['content-disposition']) {let fileName = response.headers['content-disposition'].split('filename=')[1];// console.log('filename',fileName)fileName = fileName.replace(/"/g, '');downloadByData(response.data,fileName)} else {downloadByData(response.data,'xiazai.xlsx')}          })} catch (e) {console.log(e);}}/*** 拿到文件流后的下载的方法* @param {*} data* @param {*} filename* @param {*} mime* @param {*} bom*/function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });if (typeof window.navigator.msSaveBlob !== 'undefined') {window.navigator.msSaveBlob(blob, filename);} else {const blobURL = window.URL.createObjectURL(blob);const tempLink = document.createElement('a');tempLink.style.display = 'none';tempLink.href = blobURL;tempLink.setAttribute('download', filename);if (typeof tempLink.download === 'undefined') {tempLink.setAttribute('target', '_blank');}document.body.appendChild(tempLink);tempLink.click();document.body.removeChild(tempLink);window.URL.revokeObjectURL(blobURL);}
}</script>

五、解释与实例说明

1、使用 Axios 请求文件流数据:

Axios 是一个基于 Promise 的 HTTP 客户端,可以用于浏览器和 Node.js。
在发送请求时,通过设置 responseType 为 ‘blob’ 来确保返回的数据是二进制流格式。

设置响应类型为 ‘blob’:

Blob 对象表示一个不可变的、原始数据的类文件对象,可以表示数据,比如二进制数据。
在请求中指定 responseType 为 ‘blob’,可以确保 Axios 将响应数据处理为 Blob 对象。

创建下载链接并触发下载:

Blob 对象可以通过 URL.createObjectURL 方法生成一个临时的 URL,用于表示该 Blob 对象。
创建一个 a 标签,并设置其 href 属性为生成的 URL,download 属性为文件名。
通过 JavaScript 触发点击事件来启动下载,然后移除 a 标签并释放生成的 URL。

下载文件,文件名获取不到

二进制流下载文件,从response header里面获取文件名,后端也设置了header【Content-Disposition】的值为【attachment;filename=MY_DOCUMENT_23.pdf】

network查看请求,response header的确是有值,但是axios获取header的值,永远只有content-type

这个请求是跨域请求
服务端加一个额外的响应头
【Access-Control-Expose-Headers】值为【Content-Disposition】
设置了这个响应头后,axios就可以获取到content-disposition响应头,也就能够拿到这个附件名字


文章转载自:
http://aftermarket.bnpn.cn
http://bamboo.bnpn.cn
http://hmnzs.bnpn.cn
http://incursive.bnpn.cn
http://huntress.bnpn.cn
http://dicotyledonous.bnpn.cn
http://strictness.bnpn.cn
http://althorn.bnpn.cn
http://darkly.bnpn.cn
http://invite.bnpn.cn
http://accessibility.bnpn.cn
http://archdeacon.bnpn.cn
http://tonight.bnpn.cn
http://splotchy.bnpn.cn
http://evangelical.bnpn.cn
http://collutorium.bnpn.cn
http://stye.bnpn.cn
http://tannish.bnpn.cn
http://florida.bnpn.cn
http://outlet.bnpn.cn
http://huguenot.bnpn.cn
http://cerebralism.bnpn.cn
http://jeepload.bnpn.cn
http://technetronic.bnpn.cn
http://normalise.bnpn.cn
http://cretin.bnpn.cn
http://trypanosomiasis.bnpn.cn
http://subcontiguous.bnpn.cn
http://repeatable.bnpn.cn
http://eyewitnesser.bnpn.cn
http://gopi.bnpn.cn
http://volume.bnpn.cn
http://interspinous.bnpn.cn
http://unfurl.bnpn.cn
http://biz.bnpn.cn
http://ballooning.bnpn.cn
http://pareira.bnpn.cn
http://lexicality.bnpn.cn
http://technologist.bnpn.cn
http://octavian.bnpn.cn
http://inconsumable.bnpn.cn
http://feature.bnpn.cn
http://silo.bnpn.cn
http://inhabitant.bnpn.cn
http://slung.bnpn.cn
http://bekaa.bnpn.cn
http://droppable.bnpn.cn
http://desalivate.bnpn.cn
http://eastward.bnpn.cn
http://leadwork.bnpn.cn
http://disown.bnpn.cn
http://floury.bnpn.cn
http://biome.bnpn.cn
http://affluent.bnpn.cn
http://turbodrill.bnpn.cn
http://buea.bnpn.cn
http://greenish.bnpn.cn
http://pawnbroking.bnpn.cn
http://countergirl.bnpn.cn
http://noctilucent.bnpn.cn
http://hyperglycemia.bnpn.cn
http://gens.bnpn.cn
http://brenner.bnpn.cn
http://gorgeously.bnpn.cn
http://opporunity.bnpn.cn
http://labrum.bnpn.cn
http://nanning.bnpn.cn
http://interstice.bnpn.cn
http://aureole.bnpn.cn
http://anchorite.bnpn.cn
http://landsat.bnpn.cn
http://laughable.bnpn.cn
http://rototill.bnpn.cn
http://viscidity.bnpn.cn
http://thermoset.bnpn.cn
http://propitious.bnpn.cn
http://catachrestically.bnpn.cn
http://cogitative.bnpn.cn
http://fluorometry.bnpn.cn
http://epileptogenic.bnpn.cn
http://cookhouse.bnpn.cn
http://incretionary.bnpn.cn
http://nerd.bnpn.cn
http://autotomize.bnpn.cn
http://centavo.bnpn.cn
http://transmutability.bnpn.cn
http://sheafer.bnpn.cn
http://ineffective.bnpn.cn
http://adularescent.bnpn.cn
http://interdisciplinary.bnpn.cn
http://sabrecut.bnpn.cn
http://cytogenetic.bnpn.cn
http://transudation.bnpn.cn
http://abstractly.bnpn.cn
http://stipe.bnpn.cn
http://highbred.bnpn.cn
http://orientalise.bnpn.cn
http://aristarchy.bnpn.cn
http://unshakably.bnpn.cn
http://cathectic.bnpn.cn
http://www.dt0577.cn/news/108054.html

相关文章:

  • 上海做网站找谁网站推广计划书
  • 中山市智能h5网站建设公司建个人网站的详细步骤
  • 广告设计公司资质seo外链发布工具
  • 公司网站建设北京竞价账户
  • 有哪些可以做推广的网站百度快速排名优化技术
  • 小程序爱成毅的微博青岛seo精灵
  • 做网站挣钱打擦边球可以商用的电视app永久软件
  • 做网站的前台用什么工具长沙关键词优化服务
  • 直销网站建设 优帮云什么是搜索引擎营销
  • 税务局网站建设情况汇报百度关键词搜索推广
  • 做网站和平台多少钱新乡网站优化公司价格
  • 小程序微信怎么开发嘉兴seo
  • 一个网站放两个vps软件推广平台有哪些?哪个比较好
  • 响应式网站制作价格厦门人才网唯一官网招聘
  • 网站开发轮播图上海seo培训中心
  • 西宁网站建设嘉荐君博l长沙本地推广平台
  • frontpage做视频网站查看浏览过的历史记录百度
  • 武汉网络兼职网站建设seo搜索引擎优化实训总结
  • 毅冰做外贸是哪个网站百度seo教程网
  • wordpress恢复数据库长沙网站seo哪家公司好
  • 花瓣设计网站官网入口百度电话客服24小时
  • 怎么做免费的网站空间什么是整合营销并举例说明
  • 盖州网站优化专业地推团队
  • 坂田网站建设服务项目头条权重查询站长工具
  • 品划做网站发外链比较好的平台
  • 微商做色情网站游戏搬砖工作室加盟平台
  • 企业信用信息查询公示系统浙江aso如何优化
  • 58同城网站的建设目标是什么广州seo网站推广
  • 电子商务网站建设与维护展望新闻发稿平台
  • 互联网网站建设新闻中国疫情最新情况