Home > Enterprise >  Firebase storage can't move/upload file when change its name, react js, redux
Firebase storage can't move/upload file when change its name, react js, redux

Time:09-21

I'm trying to re-upload/move a file is already existed in firebase storage to another path in the same firebase Storage with different folder and changing its name using redux-actions, so the file getting uploaded but it corrupted => it means when I try to open it, it's opening with no data/picture and the size of the picture 9B the code:

// 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);

the result from the filesToUpload from the original one which works well, these are when the first time I upload:

object for the original file

the result from the filesToUpload from the one which I want to re-upload from firebase storage to another path in firestore storage, these are when the I'm trying to re-upload to different path:

enter image description here

Anyone experience handling/moving a file from a path to another and changing its name, using react js actions, firebase storage, not node js.

CodePudding user response:

The functions to move a file are in the server-side libraries. If you want to move a file in the client you'll have to take the following steps:

1. Download the file

2. Upload the file to the new location

3. Delete the previous file (if required)

It looks like you have an idea as to how to upload the new file and delete the previous one, but are having problems downloading the previous file (which is why a 9B file is uploading).

As per the documentation, you would download the file like this

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
  });
  • Related