等待用户完成Java语言中的Blob下载

编程入门 行业动态 更新时间:2024-10-07 00:21:40
本文介绍了等待用户完成Java语言中的Blob下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Javascript中,我正在创建许多斑点,以提示用户将其另存为文件.目前,我正在使用 URL.createObjectURL 将URL放置在链接中,并模拟对链接的点击.(当然,我调用 URL.revokeObjectURL 来释放URL,以便在用户保存它后可以将其丢弃.)我有一个循环运行,并为我希望用户对每个对象执行此操作保存.

In Javascript, I'm creating a number of blobs that I want to prompt the user to save as files. At the moment, I'm doing that using URL.createObjectURL, placing the URL in a link, and simulating a click to the link. (Of course I call URL.revokeObjectURL to release the URL so the blob can be discarded after the user saves it.) I have a loop that runs through and does this for each blob I want the user to save.

至少在Firefox上,触发链接是异步操作;我调用 click(),然后在用户选择保存文件的位置之前,我的循环会立即继续执行以生成其余的blob.

At least on Firefox, triggering the link is an asynchronous operation; I call click() and my loop to generate the rest of the blobs immediately continues execution before the user chooses a location to save the file.

这带来了一个问题,因为每个斑点可能都很大,并且可能有很多.我可以很容易地预见到这样一种情况:没有足够的内存将所有内存都保存在内存中,以等待用户一张一张地保存和释放它们.因此,我想生成一个Blob,让用户下载它,然后 保存文件(因此有资格被垃圾收集器丢弃)后,继续生成下一个Blob,并让用户保存它.

This presents a bit of a problem because each blob could be somewhat large, and there could be a lot of them. I can easily foresee a situation where there wouldn't be sufficient memory to hold all of them in memory waiting for the user to save and release them one-by-one. So I'd like to generate one blob, have the user download it, and then, after the file has been saved (and thus is eligible to be discarded by the garbage collector), proceed to generate the next blob and have the user save it.

有什么方法可以实现这一功能,而无需用户手动单击另一个按钮或类似的东西来通知页面他已完成文件的保存?

Is there any way to achieve this without requiring the user to manually click another button or some such thing to signal the page that he is done saving the file?

推荐答案

这对我有用.

/** * * @param {object} object A File, Blob, or MediaSource object to create an object URL for. developer.mozilla/en-US/docs/Web/API/URL/createObjectURL * @param {string} filenameWithExtension */ function saveObjectToFile (object, filenameWithExtension) { console.log('saveObjectToFile: object', object, 'filenameWithExtension', filenameWithExtension) const url = window.URL.createObjectURL(object) console.log('url', url) const link = document.createElement('a') link.style.display = 'none' link.href = url link.target = '_blank' link.download = filenameWithExtension console.log('link', link) document.body.appendChild(link) link.click() window.setTimeout(function () { document.body.removeChild(link) window.URL.revokeObjectURL(url) }, 0) } /** * @param {string} path * @param {object} payload * @param {string} filenameWithExtension * @returns {promise} */ export async function downloadFileInBackground (path, payload, filenameWithExtension) { console.log('downloadFileInBackground awaiting fetch...') const response = await fetch(path, { method: 'POST', cache: 'no-cache', headers: { 'Content-Type': 'application/json' }, body: payload // body data type must match "Content-Type" header }) console.log('response', response) const blob = await response.blob() saveObjectToFile(blob, filenameWithExtension) console.log('downloadFileInBackground finished saving blob to file!', filenameWithExtension) }

我知道其他人也有这个问题:

I know other people have this problem too:

  • stackoverflow/a/55705784/470749
  • stackoverflow/a/372​​40906/470749
  • stackoverflow/a/30708820/470749
  • 等待用户完成使用Javascript下载Blob
  • stackoverflow/a/50698058/470749

P.S.我感谢@sicking在>://://github/whatwg/html/issues/954#issue-144165132 .

P.S. I appreciate the idea from @sicking at github/whatwg/html/issues/954#issue-144165132.

更多推荐

等待用户完成Java语言中的Blob下载

本文发布于:2023-11-13 02:48:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   用户   Java   Blob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!