使用 HTML5 文件上传 API 进行断点续传 -
我想用 Html5 实现可续传上传,除了确定要上传的文件与已部分上传的文件相同之外,一切似乎都有效。理想情况下,我会对文件进行一些客户端哈希,以生成唯一的 id - 但是我找不到它的任何工作实现,而且对于大文件来说它似乎太慢了(就像我正在处理的那样)。
接下来,我考虑使用文件的路径作为唯一标识符,但我没有看到任何从 API 获取它的方法。文件名,即使考虑每个用户的文件名也不能是唯一的,因为用户往往有通用的文件名。任何人都可以想到解决方案吗?
I want to implement resumable uploads with Html5, and everything seems to work, except identifying that the file to be uploaded is the same one already partially uploaded. Ideally, I would have some client side hashing of the file that generates unique id - however I could not find any working implementation of it and it seems too slow for large files (like I am dealing with).
Next, I thought about using the path to the file as the unique identifier, but I didn't see any way to get it from the API. file name, even considering file name per user can't be unique because users tend to have common names for files. Anyone can think of a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你最好的选择是一些简单的散列(MD5 在大文件上太慢而无用)。
另一种方法:按文件名命名上传(以及上传多个同名文件时的某种连续编号,例如 file-1、file-2、file-3),并检查已上传文件中的随机字节和本地文件。例如:
当然,这可能会导致错误的文件,但散列也可能会导致错误。
I guess you best choice is some simple hashing (MD5 is properly too slow on large files to useful).
An alternative: Name the uploads by the file name (and some kind of running numbering when multiple files with the same name is uploaded, e.g. file-1, file-2, file-3), and check random bytes in the already uploaded file and the local file. For example:
Of course this could lead to the wrong file, but so can hashing.
正如 Mozilla Hacks 的如何恢复暂停或损坏的文件上传中提到的,也许您可以使用 IndexedDB 将文件存储在浏览器中,以便您可以在不需要用户的情况下恢复上传重新选择文件。
有关如何使用 IndexedDB 存储文件的教程,请参阅 在 IndexedDB 中存储图像和文件。
As mentioned in How to resume a paused or broken file upload at Mozilla Hacks, perhaps you can store the file in the browser using IndexedDB so that you can resume the upload without needing the user to reselect the file.
For a tutorial on how to storing files with IndexedDB, see Storing images and files in IndexedDB.