【问题标题】:How to force file download on Instagram and Tik Tok in-app browsers如何在 Instagram 和 Tik Tok 应用内浏览器上强制下载文件
【发布时间】:2021-04-09 16:55:14
【问题描述】:

这在大多数浏览器中都可以正常工作:

<a href="file url" target="_blank" download>Click to download</a>

当有人点击时,文件被下载。

但当有人使用 Instagram 或 Tik Tok 应用内浏览器时,文件会在浏览器上打开且无法下载。

【问题讨论】:

标签: javascript html browser instagram anchor


【解决方案1】:
<a download="name_of__file" href="path/to/the//file"> This will do</a>

如果您知道 URL,这应该可以。不然就复杂多了

来自网络

 function downloadFile(data, fileName, type="text/plain") {
 // Create an invisible A element
 const a = document.createElement("a");
 a.style.display = "none";
 document.body.appendChild(a);

  // Set the HREF to a Blob representation of the data to be downloaded
  a.href = window.URL.createObjectURL(
   new Blob([data], { type })
   );

   // Use download attribute to set set desired file name
   a.setAttribute("download", fileName);

   // Trigger the download by simulating click
   a.click();

   // Cleanup
   window.URL.revokeObjectURL(a.href);
    document.body.removeChild(a);
   }

【讨论】:

    猜你喜欢
    • 2011-09-25
    • 2010-12-27
    • 1970-01-01
    • 2011-11-19
    • 2018-05-15
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2020-03-13
    相关资源
    最近更新 更多