HTTP POST image for Ruby on Rails application

I have a byte array image in java. I am trying to load this image into my Ruby on Rails application that uses the pearl Paperclip. My Rails model looks like this:

class App < ActiveRecord::Base has_attached_file :image end 

When I execute java code, I get an HHTTP/1.1 302 Found response to my rails application. Updated HTTP-Post java method. Here's the new java code I'm using for HTTP Post:

 public void postFile(byte[] image) throws ClientProtocolException, IOException{ HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost:3000/apps"); ContentBody cb = new ByteArrayBody(image, "image/jpg", "icon.jpg"); //ContentBody cb = new InputStreamBody(new ByteArrayInputStream(image), "image/jpg", "icon.jpg"); MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpentity.addPart("image", cb); httppost.setEntity(mpentity); HttpResponse response = httpclient.execute(httppost); System.out.println(response.getStatusLine()); } 

Here's the new debugging on the rails console:

 Started POST "/apps" for 127.0.0.1 at Mon Jan 31 00:26:07 +0000 2011 Processing by AppsController#create as HTML Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x1041491c8 @content_type=nil, @original_filename="icon.jpg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"icon.jpg\"\r\n", @tempfile=#<File:/var/folders/NU/NUcfnxwVHmmnVUo9JUdNok+++TI/-Tmp-/RackMultipart20110131-3735-1ylugl7-0>>} SQL (0.1ms) BEGIN SQL (1.0ms) describe `apps` AREL (0.4ms) INSERT INTO `apps` (`appName`, `image_file_size`, `created_at`, `image_updated_at`, `image_file_name`, `image_content_type`, `updated_at`) VALUES (NULL, NULL, '2011-01-31 00:26:07', NULL, NULL, NULL, '2011-01-31 00:26:07') [paperclip] Saving attachments. SQL (1.3ms) COMMIT Redirected to http://localhost:3000/apps/36 Completed 302 Found in 18ms 

Downloading images directly from the browser works like a charm. Here's the implementation of controller / apps_controller.rb:

 def create @app = App.new(params[:app]) respond_to do |format| if @app.save format.html { redirect_to(@app, :notice => 'App was successfully created.') } format.xml { render :xml => @app, :status => :created, :location => @app } format.json { render :json => @app, :status => :created, :location => @app } else format.html { render :action => "new" } format.xml { render :xml => @app.errors, :status => :unprocessable_entity } format.json { render :json => @app.errors, :status => :unprocessable_entity } end end end 

Where am I mistaken?

+6
java ruby-on-rails-3 paperclip
source share
1 answer

I think the problem is that you are trying to create an application with the [: app] parameters when there are no parameters with the app name, only for the image. I think the following in your action should work instead:

 @app = App.new(:image => params[:image]) 

Another good thing is to add the following validation to your application model to make sure that the record is not saved in the database if the image was not saved.

 validates_attachment_presence :image 
+2
source

All Articles