从上传的Andr​​oid图像(与Android异步HTTP客户端)到Rails服务器(使用回形针)回形针、客户端、图像、上传

由网友(丘比特,别射我)分享简介:我要上传通过HTTP POST方法的图像文件从Android设备到Rails服务器。但是,发布图片不能正常工作。更具体地,交参数(包括图像文件)似乎没有被正确地发送。I'm trying to upload an image file via http post method from android device...

我要上传通过HTTP POST方法的图像文件从Android设备到Rails服务器。 但是,发布图片不能正常工作。更具体地,交参数(包括图像文件)似乎没有被正确地发送。

I'm trying to upload an image file via http post method from android device to rails server. But posting an image is not working. More specifically, the post parameter (including image file) doesn't seem to be sent correctly.

我使用的是Android的异步HTTP客户端(http://loopj.com/android-async-http/)发布的图像从机器人,和code张贴形象是这样的。

I'm using Android Asynchronous Http Client (https://p.xsw88.cn/allimgs/daicuo/20230912/6182.png.jpg")); AsyncHttpClient client = new AsyncHttpClient(); client.post("http://x.x.x.x:3000/pictures/", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { Log.w("async", "success!!!!"); } }); }

对于轨道回形针的应用程序,我只是用脚手架和生成的模型命名为照片,并补充就可以了回形针附加文件。模型和控制器(接收请求)就像下面的。

As for rails paperclip application, I simply used scaffold and generated model named "pictures" and added paperclip attached file on it. Model and controller (which receives the request) is like below.

模式

class Picture < ActiveRecord::Base                                                                                                                                            
  attr_accessible :name, :image
  has_attached_file :image,
                    :styles => { :original => "480x480>", :thumbnail => "100x100>" },
                    :path => ':rails_root/public/system/pictures/image/:id_partition/:style_:filename'
end

控制器

# POST /pictures                                                                                                                                                              
# POST /pictures.json
def create
  @picture = Picture.new(params[:picture])

  respond_to do |format|
    if @picture.save
      format.html { redirect_to @picture, notice: 'Picture was successfully created.' }
      format.json { render json: @picture, status: :created, location: @picture }
    else
      format.html { render action: "new" }
      format.json { render json: @picture.errors, status: :unprocessable_entity }
    end
  end
end

当接收到请求,轨道服务器控制台说像以下

When receiving the request, "rails server" console says something like following

Started POST "/pictures/" for y.y.y.y at 2012-09-03 18:23:00 +0900
Processing by PicturesController#create as HTML
  Parameters: {"picture"=>{"name"=>"PictureName"}}
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "pictures" ("album_id", "created_at", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "name", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["album_id", nil], ["created_at", Mon, 03 Sep 2012 09:23:00 UTC +00:00], ["image_content_type", nil], ["image_file_name", nil], ["image_file_size", nil], ["image_updated_at", nil], ["name", "PictureName"], ["updated_at", Mon, 03 Sep 2012 09:23:00 UTC +00:00], ["user_id", nil]]
[paperclip] Saving attachments.
   (11.0ms)  commit transaction
Redirected to http://x.x.x.x:3000/pictures/10                                                                                                                               
Completed 302 Found in 15ms (ActiveRecord: 11.6ms)

正如你所看到的,只有一个paramater发送和图片[图]这是没有接收原始图像数据的参数。谁能帮我?

As you can see, there's only one paramater sent and "picture[image]" parameter which is raw image data is not received. Can anyone help me?

推荐答案

对不起,那是我的愚蠢的错误。 我应该做的。

Sorry, that was my silly mistake. I should have done

params.put("picture[image]", new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));

不是

params.put("picture[image]", File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));

应该使用新文件没有文件的

在使用安卓异步HTTP客户端( http://loopj.com/android-async-http/ ),我们不必在意MultipartEntity。 谢谢谁回答我的问题的所有球员!!!!

When using Android Asynchronous Http Client (http://loopj.com/android-async-http/), we don't have to care about MultipartEntity. Thank you to all guys who answered my question!!!!

阅读全文

相关推荐

最新文章