将文件上传到firebase

时间:2017-02-01 05:31:25

标签: ios objective-c nsfilemanager firebase-storage

NSString *filetotest=[NSString stringWithFormat:@"/var/mobile/Containers/Data/Application/3A203232-06BF-41CB-8456-1F2341C06934/Documents/"];
NSString *filessss=[NSString stringWithFormat:@"/var/mobile/Containers/Data/Application/3A203232-06BF-41CB-8456-1F2341C06934/Documents/100445.jpg"];

// File located on disk
NSURL *localFile = [NSURL URLWithString:filessss];

[@"100445.jpg" writeToFile:filetotest atomically:YES];


// Get a reference to the storage service using the default Firebase App
FIRStorage *storage = [FIRStorage storage];

// Create a storage reference from our storage service
FIRStorageReference *storageRef = [storage referenceForURL:@"gs://gnts-xxxxx.appspot.com"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"Test"];

// Upload the file to the path "images/rivers.jpg"
FIRStorageUploadTask *uploadTask = [riversRef putFile:localFile metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
    if (error != nil) {
        // Uh-oh, an error occurred!
    } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        NSURL *downloadURL = metadata.downloadURL;
    }
}];

收到错误

CFURLResourceIsReachable失败,因为它传递了一个没有方案的URL

无法访问正文文件:/var/mobile/Containers/Data/Application/3A203232-06BF-41CB-8456-1F2341C06934/Documents/100445.jpg   错误域= NSCocoaErrorDomain代码= 4“文件不存在。”

2 个答案:

答案 0 :(得分:2)

不,我得到的路径形式每次都会更改此代码,

NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];

1。 documentsPath / var / mobile / Containers / Data / Application / 7D71C4C4-33F8-4D70-AF1E-AFDEB4634031 / Documents

2。 documentsPath / var / mobile / Containers / Data / Application / B47C0C4B-0FA1-4E85-9573-89F2D6389535 / Documents

我静态使用此文件路径,这会导致此错误。 现在我的问题通过直接应用文件路径来解决, 谢谢nynohu。

答案 1 :(得分:0)

如果您使用[NSURL fileURLWithPath:]而不是[NSURL URLWithString:]从本地加载文件。

// File located on disk
NSURL *localFile = [NSURL fileURLWithPath:filessss];