2009-10-02 41 views
0

我使用NSURLRequest將iPhone中的.zip文件發佈到Rails服務器。問題在於zip文件的內容類型在傳輸中丟失。當我從Web瀏覽器上傳相同的zip文件到Rails時,內容類型將被保留。這使我相信它與我從iPhone發送它的方式有關。有誰知道爲什麼會發生這種情況?我已經發布了下面的iPhone代碼。從iPhone向Rails發佈文件時保留內容類型

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:@「Content-Type」]; 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

我能夠通過在「內容處置」標頭移去第二換行來解決這個問題,比如:

[postData appendData: [[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"archive_file", @"export.zip"] 
             dataUsingEncoding:NSUTF8StringEncoding]]; 

我猜,雙換行表示標題的末尾和「Content-Type」標題被忽略。