admin管理员组文章数量:1415491
We are currently planning a website on which people can upload movies. When looking at YouTube you notice that some movies are uploaded twice or more times (by different users). To scale our application we're thinking about the following idea:
- User selects movie file to be uploaded
- A JavaScript will get the SHA256 hash from the file (it's more accurate then the MD5 hash) before it get's uploaded
- The website will check if the hash already exists
- If the hash doesn't exist, the file will be uploaded
- If the hash does exist a message will be prompted or a reference to the already existing version on the server will be created. This without the video being uploaded.
Q: How do we analyze a file with JavaScript in order to get the SHA256 hash, and is SHA256 good enough or should we consider SHA512 (or another algorithm)?
We are currently planning a website on which people can upload movies. When looking at YouTube you notice that some movies are uploaded twice or more times (by different users). To scale our application we're thinking about the following idea:
- User selects movie file to be uploaded
- A JavaScript will get the SHA256 hash from the file (it's more accurate then the MD5 hash) before it get's uploaded
- The website will check if the hash already exists
- If the hash doesn't exist, the file will be uploaded
- If the hash does exist a message will be prompted or a reference to the already existing version on the server will be created. This without the video being uploaded.
Q: How do we analyze a file with JavaScript in order to get the SHA256 hash, and is SHA256 good enough or should we consider SHA512 (or another algorithm)?
Share Improve this question edited Dec 4, 2011 at 1:39 Peter O. 32.9k14 gold badges85 silver badges97 bronze badges asked Dec 4, 2011 at 1:29 Wouter DorgeloWouter Dorgelo 12k12 gold badges65 silver badges80 bronze badges 03 Answers
Reset to default 6Use the HTML5 File API to read the file: http://www.html5rocks./en/tutorials/file/dndfiles. Here is a JS code for calculating SHA-256: http://www.webtoolkit.info/javascript-sha256.html
I must add that I never tried this, but it seems to be possible. Alxandr is right, this would take very long for large videos, but you may try to use the WebWorker API in order not to freeze the browser: http://html5rocks./en/tutorials/workers/basics
Putting files aside for now, if the question is actually whether it's possible to get a SHA-256 hashes in JavaScript, the answer is yes. You can either reiplement this yourself (bad idea) or use a library like the Stanford JS Crypto library (good idea).
As far as the File API goes, it is implemented in the bleeding edge version of every major desktop browser, as well as the latest Android browser shipping. iOS is not supported as it doesn't really have a filesystem. Check out caniuse. for the latest stats.
Simple answer, you can't. That is if you want to support all browsers at least. I think both Chrome and FireFox supports the reading of files on the client, and IE supports it with the help of ActiveX controls, but to get a solution that works in all browsers you have to use a plugin like Flash or Silverlight. Also, when doing file-uploads of video-magnitude (large+ files), I think going for flash or the likes from the start is a good idea anyhow, but that's just my opinion.
本文标签: javascriptHow can I analyze a file about to be uploaded before it39s actually uploadedStack Overflow
版权声明:本文标题:javascript - How can I analyze a file about to be uploaded before it's actually uploaded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745169718a2645899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论