파일을 HTTP 서버에 업로드하고 싶습니다. 나는 그것을 지금 좋아한다 :iOS, multipartform-data로 파일 업로드
NSString *boundary = @"*****";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://someUploadScript.php"]];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"Keep-Alive" forHTTPHeaderField: @"Connection"];
dispatch_async(queue, ^{
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"video.mp4\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString* outputPath = @"somePathToFile";
NSData *data = [NSData dataWithContentsOfFile:outputPath];
[postbody appendData:data];
[postbody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
previousBytesWritten = 0;
connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
});
나는 예를 들어 몇 가지 추가 데이터를 보내고 싶다. "userId"값으로 "user"를 제출했습니다. 나는 같은 배열의 어떤 종류를 보낼 싶습니다
video[user] = "userId"
video[file] = //file bytes
나는이 사용하는 HTTP multipartform 데이터처럼 할 수있는 알고 있지만 난 정말 돈은 내가 어떻게 작동하는지 이해하지 어떻게 알고 있습니다. 어떤 사람이 저에게 어떻게 설명하고 어떻게 작동하는지 설명 할 수 있습니까? 이 같은
고마워요, 작동 lika 매력 :) – AYMADA
우리는 단지 파일을 추가 매개 변수를 업로드하는 경우, Content-Length == data.length? – Zhang