0
我需要把一個.csv壓縮與gzip。要做到這一點:HTTP POST多部分/表單數據與setHTTPBody像.csv.gz
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];
文件被保存在服務器上,但我不能打開它...
一些想法?
Thks for all。
此致敬禮!