我正在为我的聊天应用程序解析。上传文件时,我会保留该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。(如果解析确实提供了无用的url或blobID,则有点奇怪
编辑:
我不使用“文件”类型的属性的原因:
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];
}
这样,您可以下载文件并拥有文件名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句