私はこの問題に1週間以上苦労しています!
iPhoneからcarrierwavegemが設定されたバックエンドRailsサーバーに画像をPOSTするのに問題があります。Webフォームを介した投稿は問題なく機能しますが、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
そしてここにobjective-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サーバーでテストされ、アップロードは成功しました。だから私の問題は私のレール/搬送波の設定に関係していると思います。血まみれの画像をiPhoneからRailsサーバーに送るのを手伝ってください。
あなたの助けは大歓迎です。