gpt4 book ai didi

iOS 解析如何通过 URL 下载文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:13 39 4
gpt4 key购买 nike

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

这是我上传文件的代码:

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

}
}];

}
}

在 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?

}

}

我没有找到通过 URL 下载文件的 API。 (如果 parse 确实提供了无用的 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.

最佳答案

您应该传输 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];
}

这样您就可以下载文件并获得文件名。

关于iOS 解析如何通过 URL 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970086/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com