在iOS中大的Base64上传 [英] Large Base64 uploads in iOS

查看:237
本文介绍了在iOS中大的Base64上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:
我需要在重新presents要上传的文件的HTTP POST参数发送基地64 CS codeD字符串。

当前方法:我使用的是ASIFormDataRequest和编码我的文件到一个字符串的base64像这样:

 的NSData *数据= [[的NSFileManager defaultManager] contentsAtPath:@我的道路在这里];
* NSString的base64Data = [数据连接codeBase64ForData]。

然而上传大文件时,应用程序运行的内存和一个死可怕的死亡!

建议解决办法:现在是否有人我怎么会去,比如说,从磁盘读取该文件,以块的形式将其转换为base64字符串,在HTTP请求转换后的base64字符串连接到一个参数?

有效,避免读取整个文件到内存中的任何方式。

任何投入将大大AP preciated!

我的http请求code全:

  NSURL * URL = [NSURL URLWithString:requestProgressURLString];
NSData的*数据= [[的NSFileManager defaultManager] contentsAtPath:evidence.uri];
* NSString的base64Data = [数据连接codeBase64ForData]。
NSURL * fileURL = [[NSURL页头] initWithString:evidence.uri];请求= [ASIFormDataRequest requestWithURL:URL]request.shouldAttemptPersistentConnection = FALSE;
[要求setShowAccurateProgress:YES];[要求setPostValue:的accessToken forKey:@的accessToken];
[要求setPostValue:[fileURL路径] lastPathComponent] forKey:@文件名];
[要求setPostValue:evidence.name forKey:@标题];
[要求setPostValue:base64Data forKey:@的FileData]; //想字符串值(带的base64 codeD)
[要求addRequestHeader:@内容类型值:@应用程序/ x-WWW的形式urlen codeD];
[要求setTimeOutSeconds:60];
[要求setUploadProgressDelegate:个体经营];
[要求setDownloadProgressDelegate:个体经营];
[要求setDelegate:个体经营];
[要求setDidFinishSelector:@selector(requestDone :)]。
[要求setDidFailSelector:@selector(requestWentWrong :)]。


解决方案

我想分裂这个问题分成两个:

1。恩code大文件为Base64块中结果
2。文件上传


1。恩code大文件为Base64块中:

找一些code,它连接codeS 的NSData 实例为Base64。然后使用一个 NSInputStream 来读取数据块的数据,连接code,并使用 NSOutputStream 来写入临时文件。我发现这个问题/答案:<一href=\"http://stackoverflow.com/questions/7920780/is-it-possible-to-base64-en$c$c-a-file-in-chunks\">Is有可能的base64 EN code块中的一个文件?使块大小3的倍数,它应该工作。你可能想这个移动到后台线程。

重要:这将创建一个临时文件,即比原来大。必须有在该时刻设备足够的空间。你开始上传后,您可以删除它。


2。文件上传

看来你有这样的已经做了。您可以使用 ASIFormDataRequest 酷似你的榜样。而且由于它使该文件(它建立多部分POST文件)的拷贝,你可以删除你的副本。

如果你想通过并行下列步骤来优化它,你需要找到另一种方式来上传分段文件。

Description: I need to send a base 64 encoded string in a HTTP POST parameter that represents a file to be uploaded.

Current method: I'm using a ASIFormDataRequest and encoding my file into a base64 string like so:

NSData *data = [[NSFileManager defaultManager] contentsAtPath:@"my path here"];
NSString *base64Data = [data encodeBase64ForData];

However when uploading large files the App runs out of memory and dies a horrid death!

Proposed solution: Does anybody now how I would go about, say, reading the file from disk, converting it to base64 strings in chunks and attaching the converted base64 string to a parameter in the HTTP request?

Effectively any way that avoids reading the whole file into memory.

Any input would be greatly appreciated!

My http request code in full:

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

解决方案

I would split this problem into two:

1. Encode large file to Base64 in chunks
2. Upload that file


1. Encode large file to Base64 in chunks:

Find some code that encodes NSData instances to Base64. Then use one NSInputStream to read data in chunks, encode it and use NSOutputStream to write to a temporary file. I found this question/answer: Is it possible to base64-encode a file in chunks? Make the chunk size a multiple of 3 and it should work. You may want to move this to background thread.

Important: This will create a temporary file, that is larger than the original. There must be enough space on device for that moment. After you begin uploading you can delete it.


2. Upload that file

It seems that you have this already done. You can use that ASIFormDataRequest exactly like in your example. And since it makes a private copy of the file (it builds the multipart POST file), you can delete your copy.

If you want to optimize it by parallelizing these steps, you will need to find another way to upload the multipart file.

这篇关于在iOS中大的Base64上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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