iOS Parse如何通过URL下载文件

OMG*_*POP 3 database parsing backend ios parse-platform

我正在使用解析来聊聊我的应用程序.当我上传文件时,我会保留网址,并将网址发送给其他用户,然后用户可以通过此网址下载文件.

这是我上传文件的代码:

+ (void)uploadBlob:(NSData *)blob fileName:(NSString *)fileName type:(NSString *)type {
    if ([self private_checkCloudForbidden]) {
        return;
    }

    if (CHOSEN_SERVER_DATABASE == ServersQuickBlox) {
        if ([Format isThumbnailWithBlobFileName:fileName]) {
            type = @"image";
        }

        NSString *qbContentType = @"";
        if ([type isEqualToString:@"image"]) {
            qbContentType = @"image/jpg";
        }

        [QBContent TUploadFile:blob fileName:fileName contentType:qbContentType isPublic:YES delegate:[CloudDelegate sharedInstance]];


    }

    else if (CHOSEN_SERVER_DATABASE == ServersParse) {

        PFFile *file = [PFFile fileWithName:fileName data:blob];

        [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error){
                NSLog(@"Error: %@", [[error userInfo] objectForKey:@"error"]);
            } else {

                [CloudHandler didUploadBlobWithFileName:file.name blobID:file.url];

            }
        }];

    }
}
Run Code Online (Sandbox Code Playgroud)

在CloudHandler didUploadBlobWithFileName方法中,我将文件的url保存为blobID.以下是使用QuickBlox时通过blobID/url下载文件的方法:

+ (void)downloadWithBlobId: (NSString *)blobID {
    if ([self private_checkCloudForbidden]) {
        return;
    }


    if (CHOSEN_SERVER_DATABASE == ServersQuickBlox) {


        [QBContent TDownloadFileWithBlobID:blobID.integerValue delegate:[CloudDelegate sharedInstance]];

    }

    else if (CHOSEN_SERVER_DATABASE == ServersParse) {

        //TODO: HOW TO DOWNLOAD FILES?

    }

}
Run Code Online (Sandbox Code Playgroud)

我没有找到通过URL下载文件的API.(如果解析确实提供无用的url或blobID,那就有点奇怪了

编辑:

我之所以不使用'file'类型的属性:

1. I can't store 'file' on local database. It has to be the blobID or URL. 

2. when I send a rich message, I can send along the blobID, so that the receiver does not have to fetch the object first before downloading the binary. 
Run Code Online (Sandbox Code Playgroud)

Eli*_*kiy 5

你应该转移PFObject而不是url.像这样:

PFObject *object; //the object you have
PFFile *soundFile = object[@"file"];
[soundFile getDataInBackgroundWithBlock:^(NSData *soundData, NSError *error) {
        if (!error) {
            [self saveSoundFileToDocuments:soundData fileName:object[@"fileName"]];
        }
    }
    progressBlock:^(int percentDone) {

    }];

- (void)saveSoundFileToDocuments:(NSData *)soundData fileName:(NSString *)fileName {
    NSString *docsDir;
    NSArray *dirPaths;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:[DataAcesss encryptText:fileName]]];
    [soundData writeToFile:databasePath atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以下载文件并拥有文件名.