http postメソッドを使用してアンドロイドデバイスからrailsサーバーに画像ファイルをアップロードしようとしています。 画像の投稿は機能していません。具体的には、postパラメータ(画像ファイルを含む)が正しく送信されていないようです。Android(Android非同期Httpクライアントを使用)から画像をアップロードする(ペーパークリップ付き)
私はアンドロイドから画像を投稿するためにAndroid非同期Httpクライアント(http://loopj.com/android-async-http/)を使用しています。画像を投稿するコードは次のようなものです。レール用として
public static void postImage(){
RequestParams params = new RequestParams();
params.put("picture[name]","MyPictureName");
params.put("picture[image]",File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.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!!!!");
}
});
}
は、私は単純に足場を使用して生成されたモデルは、「絵」と命名し、その上にクリップ添付ファイルを追加して、アプリケーションをクリップ。モデルとコントローラ(要求を受け取る)は以下のようになります。
モデル
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
要求を受け、 "レールサーバー" コンソールは、あなたが見ることができるように
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があります生画像データである「画像[画像]」パラメータは受信されない。誰でも助けてくれますか?
ありがとうございました。私は似たような質問をしていました。 –
{"status": "Error"、 "error":{"image_content_type":["is invalid"]、 "image":["is invalid"]}}誰もこの前にこの問題を抱えていた? –