【发布时间】:2014-04-24 10:01:42
【问题描述】:
我需要使用 gzip 压缩 .csv。这样做:
NSString *boundary = @"*****";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL"]];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:@"POST"];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
NSMutableData *theBodyData = [NSMutableData data];
[theBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[@"Content-Disposition: form-data; name= \"server_value_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[csvString dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"9999999999997-d4a9-a5f2-87da-e564.csv.gz\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[@"Content-Type: file/csv\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[theBodyData appendData:[csvString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[theBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *theBodyDataZip=[theBodyData gzippedData];
[request setHTTPBody:theBodyDataZip];
NSError *error = nil; NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
文件已保存在服务器中,但我无法打开它...
有什么想法吗?
谢谢大家。
最好的问候!
【问题讨论】:
标签: ios http post gzip multipartform-data