iOS解析如何通过URL下载文件 [英] iOS Parse how to download file via URL

查看:1399
本文介绍了iOS解析如何通过URL下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我上传文件的代码:

  + )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];

}
}];

}
}

在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委托:[CloudDelegate sharedInstance] ];

}

else if(CHOSEN_SERVER_DATABASE == ServersParse){

// TODO:如何下载文件?

}

}

找到API通过URL下载文件。 (如果解析提供的url或blobID是无用的,则有点奇怪。



编辑:



不要使用'file'类型的属性:

  1。我不能在本地数据库上存储'file'必须是blobID或URL。

2.当我发送一个丰富的消息,我可以沿着blobID发送,以便接收者不必在下载二进制之前先获取对象$。 b $ b


解决方案

您应该转移PFObject而不是URL。
像这样:

  PFObject * object; //你拥有的对象
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];
}

这样,您可以下载文件并拥有文件名。 / p>

I am using parse for my chatting application. When I upload files, I keep the url, and send the url to other users, who can then download files via this URL.

Here is my code for uploading files:

+ (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];

            }
        }];

    }
}

and in CloudHandler didUploadBlobWithFileName method, I will save the file's url as the blobID. And here is how I download files via the blobID/url when using QuickBlox:

+ (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?

    }

}

I didn't find the API to download file via URL. (it's a bit weird if parse does provide url or blobID that is useless

EDIT:

The reason I don't use attributes of type '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. 

解决方案

You should transfer the PFObject instead of the url. Like this:

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];
}

This way you can download the file and have the file name as well.

这篇关于iOS解析如何通过URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆