我使用NSURLRequest将.zip文件从iPhone发布到Rails服务器。问题是zip文件的内容类型在传输中丢失。当我将相同的zip文件从Web浏览器上传到Rails时,将保留内容类型。这让我相信它与我从iPhone发送它的方式有关。有谁知道为什么会这样?我在下面发布了iPhone代码。
NSString * filePath = [self filePathForExportedData]; NSMutableURLRequest * theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kExportURLString] 的CachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY]
forHTTPHeaderField:@ “内容类型”]; NSMutableData * fileData = [NSMutableData dataWithContentsOfFile:filePath]; NSMutableData * postData = [NSMutableData dataWithCapacity:[fileData length] + 512]; [postData appendData:[[NSString stringWithFormat:@“ - %@ \ r \ n”,BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:[[NSString stringWithFormat:@“Content-Disposition:form-data; name = \”%@ \“; filename = \”%@ \“\ r \ n \ r \ n”,@“archive_file” ,@“export.zip”] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:[[NSString stringWithFormat:@“Content-Type:application / zip \ r \ n \ r \ n”] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:fileData]; [postData appendData:[[NSString stringWithFormat:@“\ r \ n - % - - \ r \ n”,BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; [theRequest setHTTPBody:postData]; [self makeRequest:theRequest]; #这将发送请求
答案 0 :(得分:0)
我能够通过删除“Content-Disposition”标题中的第二个换行来解决问题,如下所示:
[postData appendData: [[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"archive_file", @"export.zip"]
dataUsingEncoding:NSUTF8StringEncoding]];
我猜测双重换行表示标题的结尾,而“Content-Type”标题被忽略。