mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 22:19:20 +08:00
fix: 修复文件上传计算hash时crypto.subtle不可用的问题
This commit is contained in:
@@ -25,7 +25,16 @@ export interface UploadOptions {
|
|||||||
*/
|
*/
|
||||||
async function calculateFileHash(file: File): Promise<string> {
|
async function calculateFileHash(file: File): Promise<string> {
|
||||||
const arrayBuffer = await file.arrayBuffer();
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
|
// 兼容不同环境的 crypto.subtle
|
||||||
|
const cryptoSubtle = typeof crypto !== 'undefined' && crypto.subtle
|
||||||
|
? crypto.subtle
|
||||||
|
: (window as any).crypto?.subtle;
|
||||||
|
|
||||||
|
if (!cryptoSubtle) {
|
||||||
|
throw new Error('当前环境不支持文件加密计算,请使用现代浏览器');
|
||||||
|
}
|
||||||
|
|
||||||
|
const hashBuffer = await cryptoSubtle.digest('SHA-256', arrayBuffer);
|
||||||
const hashArray = [...new Uint8Array(hashBuffer)];
|
const hashArray = [...new Uint8Array(hashBuffer)];
|
||||||
const hashHex = hashArray
|
const hashHex = hashArray
|
||||||
.map((b) => b.toString(16).padStart(2, '0'))
|
.map((b) => b.toString(16).padStart(2, '0'))
|
||||||
|
|||||||
Reference in New Issue
Block a user