fix: 修复文件上传计算hash时crypto.subtle不可用的问题

This commit is contained in:
小蟑螂
2026-03-27 22:14:17 +08:00
parent fa6e71faa1
commit 12074def77
@@ -25,7 +25,16 @@ export interface UploadOptions {
*/
async function calculateFileHash(file: File): Promise<string> {
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 hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))