웹 서비스를 사용하여 이미지 뷰어에서 서버로 선택한 이미지를 업로드하려고합니다.이미지 파일을 서버에 업로드하는 중 오류가 발생했습니다.
내 웹 서비스에는 사용자 ID, pwd 및 파일 이름이 필요합니다.
하지만 실행하면 파일 이름에 오류가 발생합니다.
오류는 "프로 시저에 제공되지 않은 파일 이름이 필요합니다."라는 오류가 발생합니다. 나는 여기에 파일 이름을 전달하고있다.
누구든지 코드를보고 알려 주시면 문제가 될 수 있습니까? - 감사. 다른 필드에
NSData *imageData;
imageData = UIImageJPEGRepresentation(imageView.image, 90);
//creating the url request:
NSURL *postUrl = [NSURL URLWithString:@"myURL"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postUrl];
//adding header information:
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//setting up the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"100"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"pwd\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"pwd"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *returnString;
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
lblLoginStatus.text = @"Uploading done.";
NSLog(@"%@",returnString);
이미지를 서버에 업로드하는 데 며칠을 보냈습니다. 실제로 많은 변수가 있습니다. iOS 측 대신 서버 측에서 문제가 될 수 있습니다. 몇 가지 제안 해 주시겠습니까? 1. 다음 줄을 주석 처리합니다. [postBody appendData : [[NSString stringWithString : @ "Content-Type : image/jpg \ r \ n \ r \ n"] dataUsingEncoding : NSUTF8StringEncoding]; 2. 내용 길이를 다음과 같이 추가하십시오. [addValue 요청 : [NSString stringWithFormat : @ "% d", [본문 길이] forHTTPHeaderField : @ "Content-Length"]; – John