1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| import React, { useState, useRef } from 'react'; import COS from 'cos-js-sdk-v5'; import axios from 'axios';
const TencentCosUpload = () => { const [uploadProgress, setUploadProgress] = useState(0); const [uploadStatus, setUploadStatus] = useState('idle'); const [fileUrl, setFileUrl] = useState(''); const cosClientRef = useRef<COS | null>(null); const uploadTaskRef = useRef<any>(null); const uploadInfoRef = useRef<{ bucket: string; region: string; fileName: string; uploadId: string; } | null>(null);
const initCosClient = async () => { const res = await axios.get('/api/cos/sts-token'); if (res.data.code !== 0) throw new Error(res.data.message); const { TmpSecretId, TmpSecretKey, SecurityToken, bucket, region } = res.data.data; const cos = new COS({ getAuthorization: (options, callback) => { callback({ TmpSecretId, TmpSecretKey, SecurityToken, ExpiredTime: res.data.data.ExpiredTime }); } }); cosClientRef.current = cos; return { cos, bucket, region }; };
const generateFileName = (file: File) => { const suffix = file.name.split('.').pop(); const timestamp = Date.now(); const random = Math.floor(Math.random() * 10000); return `upload/${timestamp}_${random}.${suffix}`; };
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => { const file = e.target.files?.[0]; if (!file) return;
try { setUploadStatus('uploading'); setUploadProgress(0); const { cos, bucket, region } = await initCosClient(); const fileName = generateFileName(file);
uploadTaskRef.current = cos.sliceUploadFile({ Bucket: bucket, Region: region, Key: fileName, Body: file, SliceSize: 10 * 1024 * 1024, AsyncLimit: 3, EnableResume: true, onProgress: (info) => { setUploadProgress(Math.floor(info.percent * 100)); }, onFileFinish: (err, data) => { if (err) throw err; uploadInfoRef.current = { bucket, region, fileName, uploadId: data.UploadId }; const url = `https://${bucket}.cos.${region}.myqcloud.com/${fileName}`; setFileUrl(url); setUploadStatus('success'); } }); } catch (err) { setUploadStatus('fail'); console.error('上传失败:', err); } };
const handlePause = () => { if (uploadTaskRef.current) { uploadTaskRef.current.pause(); setUploadStatus('paused'); } };
const handleResume = () => { if (uploadTaskRef.current) { uploadTaskRef.current.resume(); setUploadStatus('uploading'); } };
const handleCancel = async () => { if (!uploadInfoRef.current || !cosClientRef.current) return; try { const { bucket, region, fileName, uploadId } = uploadInfoRef.current; await cosClientRef.current.abortMultipartUpload({ Bucket: bucket, Region: region, Key: fileName, UploadId: uploadId }); uploadTaskRef.current = null; uploadInfoRef.current = null; cosClientRef.current = null; setUploadProgress(0); setUploadStatus('idle'); setFileUrl(''); } catch (err) { console.error('取消上传失败:', err); } };
return ( <div style={{ padding: 20, maxWidth: 600, margin: '0 auto' }}> <h3>腾讯云COS 分片上传&断点续传</h3> <div style={{ marginBottom: 20 }}> <input type="file" onChange={handleFileChange} disabled={uploadStatus === 'uploading'} /> </div>
{/* 上传进度 */} {uploadStatus !== 'idle' && ( <div style={{ marginBottom: 20 }}> <div style={{ width: '100%', height: 8, background: '#eee', borderRadius: 4, overflow: 'hidden' }}> <div style={{ width: `${uploadProgress}%`, height: '100%', background: '#006EFF', transition: 'width 0.3s' }} /> </div> <p>上传进度:{uploadProgress}% | 状态:{uploadStatus}</p> </div> )}
{/* 操作按钮 */} <div style={{ display: 'flex', gap: 10, marginBottom: 20 }}> {uploadStatus === 'uploading' && ( <button onClick={handlePause}>暂停上传</button> )} {uploadStatus === 'paused' && ( <button onClick={handleResume}>继续上传</button> )} {uploadStatus !== 'idle' && uploadStatus !== 'success' && ( <button onClick={handleCancel}>取消上传</button> )} </div>
{/* 上传结果 */} {fileUrl && ( <div> <p>上传成功!文件地址:</p> <a href={fileUrl} target="_blank" rel="noreferrer">{fileUrl}</a> </div> )} </div> ); };
export default TencentCosUpload;
|