我一直在爲這個問題奮鬥超過一個星期!JSON和carrierwave iphone問題
我無法將圖像從我的iphone發佈到配置了carrierwave gem的後端導軌服務器。通過網絡表單發佈工作很好,但是,通過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服務器測試過的,上傳的是成功了! 所以我想我的問題與我的rails/carrierwave設置有關。請幫助我從我的iPhone和我的Rails服務器中獲取血腥圖像。
非常感謝您的幫助。
請任何人! – iosgcd 2011-03-13 18:55:36
我仍然遇到這個錯誤使用rails 3.0.6任何其他的想法? – 2011-04-15 19:20:20