【问题标题】:Getting error "File ... couldn’t be opened because there is no such file" for exising file现有文件出现错误“文件...无法打开,因为没有这样的文件”
【发布时间】:2018-05-03 00:44:38
【问题描述】:

我在带有 iPhone 6 模拟器的 iOS 上使用 rn-fetch-blob 0.10.8 和 RN 0.49。

我正在使用 react-native-image-resizer 模块调整图像大小,然后使用 react-native-fetch-blob 将该图像移动到正确的位置。但是,这样做时我得到了错误:

文件“6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg”无法打开,因为没有这样的文件。

在进行复制之前,我将源和目标打印到控制台:

"移动文件:///Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/库/缓存/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg => /Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341- 9C60-4AA3-8D8F-69207A8C9454/Documents/testing.jpg"

我可以验证路径 //Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg 是否存在,因为我可以在 Finder 中打开它。但是由于某种原因 rn-fetch-blob 没有找到它。

知道可能是什么问题吗?

有关我正在使用的代码的信息:

const resizedImage = await ImageResizer.createResizedImage(localFilePath, dimensions.width, dimensions.height, format, 85);
const resizedImagePath = resizedImage.uri;
console.info('Moving ' + resizedImagePath + ' => ' + targetPath);
await RNFetchBlob.fs.cp(resizedImagePath, targetPath); // Throws error

【问题讨论】:

    标签: ios react-native react-native-fetch-blob


    【解决方案1】:

    问题是 JavaScript 无法访问此路径,您需要先将其保存到有效位置,例如应用程序 temp 文件夹,然后传递 temp 文件夹 URL。 这是您在 iOS 上保存到 temp 文件夹的方式:

    +(NSURL*)saveToTmpFolder:(NSData*)data {
        NSString *temporaryFileName = [NSProcessInfo processInfo].globallyUniqueString;
        NSString *temporaryFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[temporaryFileName stringByAppendingPathExtension:@"jpg"]];
        NSURL *temporaryFileURL = [NSURL fileURLWithPath:temporaryFilePath];
    
        NSError *error = nil;
        [data writeToURL:temporaryFileURL options:NSDataWritingAtomic error:&error];
    
        if ( error ) {
            //NSLog( @"Error occured while writing image data to a temporary file: %@", error );
        }
        else {
            //NSLog(@"Image Saved - YOU ROCK!");
        }
        return temporaryFileURL;
    }
    

    或者如果你想要 JavaScript API,react-native-fs 是一个很好的解决方案。

    【讨论】:

    • 是的,就是这样,我已将 RNFetchBlob.fs.cp(source, target) 替换为 RNFS.copyFile(source, target) 并且它有效。我不明白为什么一个工作而另一个不工作,因为两者都是本机模块?还是 RNFetchBlob 中的错误?无论哪种方式,感谢您为我指明正确的方向。
    猜你喜欢
    • 2016-10-07
    • 2014-11-23
    • 2016-02-18
    • 2017-09-29
    • 2014-08-25
    • 2016-07-15
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多