2011-03-10 3 views
4

1 주일 이상이 문제로 고생하고 있습니다.JSON 및 반송파 iphone 문제

캐리어 웨스턴 보석이 구성된 백엔드 레일 서버에 내 아이폰의 이미지를 게시하는 데 문제가 있습니다. 웹 양식을 통한 게시는 잘 작동하지만 JSON을 통해 아이폰에서 텍스트를 게시 할 때도 마찬가지입니다. 여기

내 사진 컨트롤러 :

class PhotosController < ApplicationController 
    def new 
    @photo = Photo.new(:user_id => params[:user_id]) 
    end 

    def create 
    @photo = current_user.photos.build(params[:photo]) 
    if @photo.save 

     respond_to do |format| 
     format.html { flash[:notice] = "Successfully created photo." 
     redirect_to @photo.user 
     } 
     format.json { 
      render :json => {:action => 'create', :owner => current_user} 
     } 
     end 
    else 
     render :action => 'new' 
    end 
    end 

여기에 목표 - C 코드입니다 : 내가 서버에서지고있어 응답이

UIImage *image = [UIImage imageNamed:@"moon.png"]; 
    NSData *imageData = UIImagePNGRepresentation(image); 

    NSString *urlString=[NSString stringWithFormat:@"http://127.0.0.1:3000/photos.json"]; 
    // setting up the URL to post to 

    // setting up the request object now 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // now lets make the connection to the web 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(returnString); 

입니다

:

"는 EOFError (나쁜 콘텐츠 본문) "

objective-c 코드는 PHP 서버에서 테스트되었고 업로드는 성공한! 내 문제는 내 레일/carrierwave 설정과 관련이 있습니다. 내 아이폰과 내 레일 서버에서 피 묻은 이미지를 얻는 걸 도와주세요.

귀하의 도움에 크게 감사드립니다.

+0

아무나하십시오! – iosgcd

+0

난 아직도 레일 3.0.6을 사용 하여이 오류가 발생 다른 생각? –

답변

2

게시를 위해 ASIHTTPRequest을 사용해 보지 않겠습니까?

멀티 파트 데이터를 게시하는 데 편리한 방법이 많이 있으며 실제로 잘 작동합니다. 그렇게하면 요청 본문을 직접 만들려고 할 때 발생할 수있는 오류로 인해 발생할 수있는 문제를 배제 할 수 있습니다 ...

+0

레일 (랙)에 버그가있었습니다. 어쨌든 고마워요 :) – iosgcd

+0

당신의 질문을 편집하거나 닫으십시오, 사람들은 이것에 그들의 시간을 낭비하지 않을 것입니다;) –