简体   繁体   English

下载锚标签会在浏览器中打开文件,但不会在React组件中下载文件

[英]download anchor tag opens file in a browser but not download it in React component

I have this in my React component where I will click a download to download a file: 我的React组件中有此文件,我将在此处单击download以下载文件:

<a
  key={file.attachmentId} className="item uploaded"
  href={Pathes.Attachments.getById(file.attachmentId)}
  target="_blank"
  download
>
  Download
</a>

My path for the file: 我的文件路径:

static Attachments = class {
  static base = baseApi('attachments/download')
  static getById = (attachmentsId: any) => baseApi(`attachments/download/${attachmentsId}`)
}

In a result, file opens in browser but not downloading. 结果,文件在浏览器中打开,但未下载。

You could try setting the HTTP headers to the following: 您可以尝试将HTTP标头设置为以下内容:

Content-Disposition: attachment; filename="filename.[yourextention]"

It's quite common if this is a PDF, browser extensions want to read rather than download! 如果这是PDF,这是很常见的,浏览器扩展程序要阅读而不是下载!

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM