2009-10-02 2 views
0

NSURLRequest를 사용하여 iPhone에서 Rails 서버로 .zip 파일을 게시하려고합니다. 문제는 zip 파일의 콘텐츠 유형이 전송 중에 손실된다는 것입니다. 동일한 zip 파일을 웹 브라우저에서 Rails로 업로드하면 컨텐츠 유형이 보존됩니다. 이것은 내가 그것이 아이폰에서 보내는 방식과 관련이 있다고 믿게한다. 왜 이런 일이 일어날 지 아는 사람이 있습니까? 아래에 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] 

HTTPHeaderField : @ "Content-Type"]; NSMutableData * fileData = [NSMutableData dataWithContentsOfFile : filePath]; NSMutableData * postData = [NSMutableData dataWithCapacity : [fileData length] + 512]; [postData appendData : [[NSString stringWithFormat : @ "- % @ \ r \ n", 경계] dataUsingEncoding : NSUTF8StringEncoding]]; [postData appendData : [[NSString stringWithFormat : @ "콘텐츠 처리 : 양식 데이터, 이름 = \"% @ \ ", 파일 이름 = \"% @ \ "\ r \ n \ r \ n", @ "archive_file ", @"export.zip "] dataUsingEncoding : NSUTF8StringEncoding]]; [postData appendData : [[NSString stringWithFormat : @ "콘텐츠 유형 : 응용 프로그램/zip \ r \ n \ r \ n"] dataUsingEncoding : NSUTF8StringEncoding]]; [postData appendData : fileData]; [postData appendData : [[NSString stringWithFormat : @ "\ r \ n - % @ - \ r \ n", 경계] 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"헤더가 무시되었습니다.

관련 문제