我需要使用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];
文件保存在服务器中,但我无法打开它......
有些想法?
为所有人而战。
祝你好运!
答案 0 :(得分:0)
这对我有用,只需替换%%
中的值POST %URI% HTTP/1.1
Authorization:Basic ****
X-HostCommonName:%HOSTNAME%
MIME-Version:1.0
Host:%HOST%
Content-Length:523
X-Target-URI:%URI%
Content-Type:multipart/form-data; type="application/vnd.ms-excel"; start="<file>"; boundary="--0246824681357ACXZabcxyz"
Connection:Keep-Alive
----0246824681357ACXZabcxyz
Content-Type: application/vnd.ms-excel; name="file"
Content-Transfer-Encoding: binary
Content-ID: <file>
Content-Disposition: form-data; name="file"; filename="usageFile.csv"
Content-Location: file
%BINARY DATA%
----0246824681357ACXZabcxyz--