2017-02-25 1 views
0

이미지를 서버에 업로드하고 싶습니다. 서버용 서비스는 .NET으로 작성되었습니다. 이제 서버에서 멀티 파트가 아닌 데이터의 바이트로 이미지를 보내도록 요청합니다. 아래 코드로 파일을 업로드하고 있습니다. 업로드 된 파일은 응답하지만 이미지는 표시되지 않습니다.객관적인 관점에서 파일을 여러 부분없이 서버에 업로드 하시겠습니까?

[request setURL:[NSURL URLWithString:url]]; 
      [request setHTTPMethod:@"POST"]; 
      NSMutableData *body = [NSMutableData data]; 
      NSString *boundary = @"---------------------------14737809831466499882746641449"; 
      //NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
      //[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

      [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[@"Content-Disposition: form-data; name=\"host_pic\"; filename=\"parkN.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:postData]; 
      [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

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

     // setting the body of the post to the reqeust 
      [request setHTTPBody:body]; 

문제가 될 수있는 이유를 설명해주세요. 어떻게해야합니까?

+0

검사 ans와 : http://stackoverflow.com/a/7289185/4831524 –

+0

를 그럼 어떻게 당신이 mycode를 업데이트하십시오 server.Can 해당 바이트 배열을 보내? – iOSGuy

+0

해당 내용을 확인하십시오.이 ans http://stackoverflow.com/a/38263070/4831524 –

답변

0

POST JSON 형식을 시도 할 수 있습니다. 이와

NSError *error; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
NSURL *url = [NSURL URLWithString:@"<YOUR SERVER>"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; 

UIImage *image = [UIImage imageNamed:@"image.png"]; 
NSString *byteArray = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 

NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: byteArray, @"host_pic", 
        @"IOS TYPE", @"typemap", 
        nil]; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; 
[request setHTTPBody:postData]; 


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

}]; 

[postDataTask resume]; 
+0

API 사람이 내게 아무 것도 보낼 필요가 없다고 말했습니다. 키를 host_pic으로 업로드 요청이 – iOSGuy

+0

이면 __multipart/form-data__ 이미지 업로드 –

+0

API는 멀티 파트 양식 데이터를 지원하지 않으며 키를 보낼 수 없습니다 – iOSGuy

관련 문제