使用react-native-fs移动文件时,文件已经存在

发布于 2025-01-26 02:13:17 字数 808 浏览 1 评论 0原文

我正在尝试使用iOS中的react-native-fs将图像移至库中,

const originalPath = "/private/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/tmp/ReactNative/E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png"

const destinationPath = RNFS.LibraryDirectoryPath + "/kj3.jpg";

        RNFS.moveFile(screenShotPath, destinationPath).then((data) => {
            console.log(data)
        }).catch((err) => {
            throw err
        })

在这种情况下,目的地路径是

/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/Library/kj3.jpg

我遇到一个错误,

Error: “E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png” couldn’t be moved to “Library” because an item with the same name already exists.

当我尝试复制文件时,我也会遇到相同的错误。

I am trying to move an image to the library using react-native-fs in iOS

const originalPath = "/private/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/tmp/ReactNative/E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png"

const destinationPath = RNFS.LibraryDirectoryPath + "/kj3.jpg";

        RNFS.moveFile(screenShotPath, destinationPath).then((data) => {
            console.log(data)
        }).catch((err) => {
            throw err
        })

The destination path in this case is

/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/Library/kj3.jpg

I get an error

Error: “E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png” couldn’t be moved to “Library” because an item with the same name already exists.

I also get the same error when I try to copy the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-02-02 02:13:18

在iOS中,如果已经存在,则不会覆盖图像。请尝试安装屏幕截图和目标路径,以确保路径确实不同。

以下是一个工作解决方案,您可以首先检查文件是否存在。如果是这样,它将尝试先删除它,然后在您要防止存在错误时移动文件。

希望它能帮助您!

RNFS.exists(path + filename)
                    .then(exists => {
                      if (exists) {
                        // If the image file exists, remove it
                        return RNFS.unlink(destinationPath);
                      } else {
                        // If the image file does not exist, do nothing
                        return Promise.resolve();
                      }
                    })
                    .then(() => {
                      // Move the image file to the new location
                      return RNFS.moveFile(screenShotPath, destinationPath);
                    })
                    .then(() => {
                      console.log('Image file moved successfully');
                    })
                    .catch(error => {
                      console.log('Error moving image file:', error);
                    });

In iOS, image will not overwrite if already exists. Please try to console.log the screenShotPath and destinationPath to make sure the path are really different.

Below is a working solution shows that you can first check the file is it exists. If so, it will try to remove it first, then it will move the file as you want to prevent any exists error.

Hope it will help you!

RNFS.exists(path + filename)
                    .then(exists => {
                      if (exists) {
                        // If the image file exists, remove it
                        return RNFS.unlink(destinationPath);
                      } else {
                        // If the image file does not exist, do nothing
                        return Promise.resolve();
                      }
                    })
                    .then(() => {
                      // Move the image file to the new location
                      return RNFS.moveFile(screenShotPath, destinationPath);
                    })
                    .then(() => {
                      console.log('Image file moved successfully');
                    })
                    .catch(error => {
                      console.log('Error moving image file:', error);
                    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文