【问题标题】:HTTP Response with Content-Disposition doesn't trigger download带有 Content-Disposition 的 HTTP 响应不会触发下载
【发布时间】:2021-12-01 21:55:14
【问题描述】:

我使用 msw 模拟后端响应以进行自动化 FE 测试。

我想为用户创建的数据创建导出功能。我正在POST发送对所需路线的请求,并希望返回带有 .csv 的响应; .pdf; .xls 文件,应自动下载。

我一直在寻找合适的方法来做到这一点,并遇到了 Content-Disposition 响应标头。

很遗憾,响应没有触发保存对话框,也没有开始下载,但我找不到错误。


编辑:经过几次对话和一些特殊情况后,我决定为 Blob-Data 创建一个 ObjectURL 生成,并使用window.open() 调用强制下载。

我更改了我的处理程序中的预期响应并添加了一个 我的 FE 中的相应调用。

handlers.js

rest.post('/mock/things/:thingId/export', async (req, res, ctx) => {
  const docType = req.headers.get('Content-type');
  const startDate = req.url.searchParams.get('startDate');
  const endDate = req.url.searchParams.get('endDate');
  const selected = req.body;

  const resDataString = 'testName,testLastName,\r\nDaniel,Schaller';
  const blob = new Blob([resDataString], { type: docType });
  const buffer = await blob.arrayBuffer();

  return res(
    ctx.status(200),
    ctx.set({
      'content-type': `${docType}`,
      'Content-length': buffer.byteLength.toString(),
    }),
    ctx.body(buffer),
    ctx.delay(),
  );
}),

store-module.js

async exportThings(ctx, exportData) {
  const {
    docType, startDate, endDate, selected,
  } = exportData;

  const res = await services.exportThingsForStuff(ctx.state.id, docType, startDate, endDate, selected);

  if (res.data !== undefined) {
    const dataBLOB = new Blob([res.data], { type: res.headers['content-type'] });
    window.open(URL.createObjectURL(dataBLOB));
  }
},

让我向您展示工作流程:

some-component.vue

async submitExport() {
  const exportDataFilters = {
    docType: this.docType,
    startDate: this.startDate,
    endDate: this.endDate,
    selected: this.selected,
  };

  const response = await this.$store.dispatch('exportThings', exportDataFilters);
  console.log(response);
},

store-module.js

async exportThings(ctx, exportData) {
  const {
    docType, startDate, endDate, selected,
  } = exportData;

  return services.exportThingsForStuff(ctx.state.id, docType, startDate, endDate, selected);
},

services.js

export const exportThingsForStuff = async (thingsId, docType, startDate, endDate, pl) => axios.post(`/mock/things/${thingsId}/export`, pl, {
  headers: {
    'Content-type': docType,
  },
  params: {
    startDate,
    endDate,
  },

最后是来自 handlers.js 的 MSW 响应函数

rest.post('/mock/things/thingId/export', (req, res, ctx) => {
  // Vars will be in use to mimic filter behavior, as soon as the download problem is solved
  const docType = req.headers.get('Content-type');
  const startDate = req.url.searchParams.get('startDate');
  const endDate = req.url.searchParams.get('endDate');
  const selected = req.body;


  
// Creating a test string and blobbing it.
const resDataString = 'testName,testLastName,\r\nJoe,Moe';
const blob = new Blob([resDataString], { type: 'text/csv' });

return res(
  ctx.status(200),
  ctx.set({
    'Content-Disposition': 'attachment; filename="things_export.csv"',
    'Content-Length': blob.size.toString(),
    'Content-Transfer-Encoding': 'binary',
    'Content-Type': 'text/csv',
  }),
  ctx.body(blob),
  ctx.delay(),
);

}),

我找不到它的问题,因为响应看起来像预期的那样:

Request URL: http://localhost:8080/mock/things/101/export?startDate=&endDate=
Request Method: POST
Status Code: 200 OK (from service worker)
Referrer Policy: strict-origin-when-cross-origin

RESPONSE HEADERS
content-disposition: attachment; filename="things_export.csv"
content-length: 37
content-transfer-encoding: binary
content-type: text/csv
x-powered-by: msw

【问题讨论】:

  • 您可以尝试在 ctx.body 中发送文本而不是 Blob 吗?
  • @kettanaito 我几乎尝试了所有方法。 HTML、纯文本、字符串化 HTML 和 blob。可惜结果没有改变。在每种情况下,我都会收到包含正确数据的响应,但下载并未按预期触发。
  • content-transfer-encoding 是 MIME 标头,而不是 http 标头。要强制下载,您的内容类型标头值应为 application/octet-stream
  • 拜托,您能否尝试在configuring 时提供responseType: 'blob' exportThingsForStuff 方法中的axios POSTexport const exportThingsForStuff = async (thingsId, docType, startDate, endDate, pl) => axios.post(/mock/things/${thingsId}/export, pl, { headers: { 'Content-type': docType, }, params: { startDate, endDate, }, responseType: 'blob' .请问,你能试试吗?
  • 请考虑审查this related so question,我认为这可能会有所帮助

标签: javascript axios xmlhttprequest http-headers msw


【解决方案1】:

据我了解,您正在使用来自您的 Javascript 应用程序的链接的响应,对吗?在这种情况下,浏览器不会打开下载对话框。 您应该将用户重定向到链接以触发下载对话框。但是您想使用 POST 方法,而不是填写表单并以编程方式提交,而不是调用 axios.post

【讨论】:

  • 感谢您尝试提供答案。我选择创建一个 ObjectURL 并使用 window.open(ObjectURL) 打开它,因为不需要文件的初始命名。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2021-07-20
  • 2010-11-03
  • 2020-04-02
  • 2013-02-10
  • 2011-11-19
  • 1970-01-01
  • 2017-10-10
相关资源
最近更新 更多