2012-02-11 1 views
0
NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"http://test.com/upload.php"]]; 

    [post setHTTPMethod: @"POST"]; 


    NSString *boundary = [NSString stringWithString:@"---------------------------  3587343183674357"]; 

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

[post addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


body = [NSMutableData data]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"textfile\"; filename=\"test.txt\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"Content-Type: application/multipart/form-data \r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


NSString* str= @"teststring"; 
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; 
[body appendData:data]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


[post setHTTPBody:body]; 

텍스트 파일을 보낸 후 내 서버를 검사하고 teststring을 보내 매개 변수의 일부로 첨부합니다. 아니, 사실 그것은 test.txt라는 파일에 있고 디렉토리에 있어야합니다. 그래서 어떻게 그 데이터를 test.txt 파일에 넣고 디렉토리에 보낼 수 있습니까? 매개 변수의 일부로 만 사용하고 싶지 않습니다.IPhone HTTP 게시 텍스트 데이터 및 텍스트 파일로 저장됩니다.

답변

0

문서 디렉토리에 데이터를 쓰시겠습니까? 다음 코드를 사용하십시오 ...

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
[data writeToFile:[NSString stringWithFormat:@"%@/test.txt", documentsDirectory] atomically:YES]; 
+0

아니요, 아이폰에 보여주는 것은 글입니다. 내가 뭘 하려는지 아이폰에서 HTTP 서버에 HTTP 게시물 작업을 통해 데이터를 보내는 것입니다. test.txt는 궁극적으로 컴퓨터에 연결되어야합니다. – lilzz

+0

아마 약간 이해하기 쉽게 질문을 편집 할 수 있습니다 ... – SEG

관련 문제