说明: 我需要在 HTTP POST 参数中发送一个 base 64 编码的字符串,该参数表示要上传的文件。
当前方法:我正在使用 ASIFormDataRequest 并将我的文件编码为 base64 字符串,如下所示:
NSData *data = [[NSFileManager defaultManager] contentsAtPath:@"my path here"];
NSString *base64Data = [data encodeBase64ForData];
但是,当上传大文件时,应用程序会耗尽内存并死于可怕的死亡!
建议的解决方案:现在有人知道我将如何处理,例如,从磁盘读取文件,将其转换为块中的 base64 字符串并将转换后的 base64 字符串附加到 HTTP 请求中的参数吗?
有效地避免将整个文件读入内存的任何方式。
任何投入将不胜感激!
我的完整http请求代码:
NSURL *url = [NSURL URLWithString:requestProgressURLString];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:evidence.uri];
NSString *base64Data = [data encodeBase64ForData];
NSURL *fileURL = [[NSURL alloc] initWithString:evidence.uri];
request = [ASIFormDataRequest requestWithURL:url];
request.shouldAttemptPersistentConnection = false;
[request setShowAccurateProgress:YES];
[request setPostValue:accessToken forKey:@"AccessToken"];
[request setPostValue:[[fileURL path] lastPathComponent] forKey:@"Filename"];
[request setPostValue:evidence.name forKey:@"Title"];
[request setPostValue:base64Data forKey:@"FileData"]; //wants string value (base64 encoded)
[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request setTimeOutSeconds:60];
[request setUploadProgressDelegate:self];
[request setDownloadProgressDelegate:self];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];