reactjs - Firebase存储更改名称时无法移动/上传文件,反应js,redux
问题描述
我正在尝试将firebase存储中已经存在的文件重新上传/移动到具有不同文件夹的同一firebase存储中的另一个路径并使用redux-actions更改其名称,因此文件已上传但已损坏=>这意味着当我尝试打开它时,它打开时没有数据/图片和图片的大小9B
代码:
// upload the same file with new path and remove the old file
let uploadAndDeletePromises = fileArr.map(
(fileData, index) =>
storageRef
// newpaths=folderName/FolderWithIdName/fileName__docId.png
.child(newFilesPaths[index])
.put(
// filesToUpload data showed in the pictures below
filesToUpload[index],
// metadata={customMetadata:
// {uplaoderId:"",PrId:""}
metadata[index]
)
.then(() =>
//remove old path
storageRef
// fileData.path -> the old path
// FolderName/folderWithIdName/fileName.png
.child(fileData.path)
.delete()
)
);
return Promise.all(uploadAndDeletePromises);
原始结果的结果filesToUpload
效果很好,这些是我第一次上传的时候:
结果来自filesToUpload
我想从firebase存储重新上传到firestore存储中的另一条路径的结果,这些是我试图重新上传到不同路径的时候:
任何人都经历过使用 react js 操作、firebase 存储而不是 node js 处理/移动文件从一个路径到另一个路径并更改其名称。
解决方案
移动文件的函数位于服务器端库中。如果要在客户端中移动文件,则必须执行以下步骤:
1.下载文件
2.上传文件到新位置
3.删除之前的文件(如果需要)
您似乎知道如何上传新文件并删除前一个文件,但在下载前一个文件时遇到问题(这就是上传 9B 文件的原因)。
根据文档,您可以像这样下载文件
storageRef.child('path/to/file').getDownloadURL()
.then((url) => {
// `url` is the download URL for the file
// This can be downloaded directly:
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = (event) => {
var blob = xhr.response;
// insert upload and delete code here
};
xhr.open('GET', url);
xhr.send();
})
.catch((error) => {
// Handle any errors
});
推荐阅读
- json - 从 api fetch 调用映射数组的问题
- c# - Directory.Delete - 如何判断文件夹是否为空与其他错误?
- qml - QML:contentItem 不显示任何图像
- db2 - 将 StringTimestamp 数据阶段转换为 Timestamp db2
- html - 将基于列的网格变成基于行的网格
- powershell - 如何使用 PowerShell 为 OAuth 公共客户端编码 PKCE 代码
- python - Python3 tkinter 询问目录
- delphi - 来自 IOTAModuleCreator 的 de NewImplSource 是否会自动“显示”IDE 上的新单元?
- git - IntelliJ + git 推送失败:“未将分支主机设置为自己的上游”
- arrays - 为表格上的每个项目和另一个工作表上的其他几个单元格填充工作表上的一行