七牛上传文件 重新生成hash 但是文本图像还是之前的图像 请给出代码 react
时间: 2024-04-19 19:25:43 浏览: 15
以下是使用 React.js 和七牛云的 JavaScript SDK 进行文件上传和哈希值生成的示例代码:
```jsx
import React, { useState } from "react";
import { Upload, message } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import { QiniuClient } from "qiniu";
const accessKey = "your_access_key";
const secretKey = "your_secret_key";
const bucketName = "your_bucket_name";
const qiniuClient = new QiniuClient(accessKey, secretKey);
const App = () => {
const [fileList, setFileList] = useState([]);
const handleFileChange = (info) => {
let fileList = [...info.fileList];
// 上传文件
if (info.file.status === "done") {
const file = info.file.originFileObj;
const key = "new/image.jpg"; // 上传后的文件名
qiniuClient.upload(file, key, token).then(
(response) => {
// 生成新的哈希值
const newHash = response.hash;
console.log("新的哈希值:", newHash);
// 更新文件列表
fileList = fileList.map((file) => {
if (file.uid === info.file.uid) {
return {
...file,
newHash: newHash,
};
}
return file;
});
setFileList(fileList);
},
(error) => {
console.log("文件上传失败", error);
}
);
}
setFileList(fileList);
};
return (
<div>
<Upload
onChange={handleFileChange}
fileList={fileList}
beforeUpload={() => false}
>
<button>
<UploadOutlined /> 选择文件
</button>
</Upload>
</div>
);
};
export default App;
```
请注意,上述代码中的 `your_access_key`、`your_secret_key` 和 `your_bucket_name` 需要替换为你自己的七牛云账号信息。`new/image.jpg` 需要替换为你上传后的文件名。在成功上传文件后,可以通过 `response.hash` 获取新的哈希值,并将其更新到文件列表中。