![](/img/trans.png)
[英]Uploading an image from Android (with Android Asynchronous Http Client) to laravel server
[英]Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)
我正在尝试通过http post方法从android设备上传图像文件到rails服务器。 但发布图像不起作用。 更具体地说,post参数(包括图像文件)似乎没有正确发送。
我正在使用Android异步Http客户端(http://loopj.com/android-async-http/)从android发布图像,发布图像的代码是这样的。
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!!!!");
}
});
}
对于rails paperclip应用程序,我只使用了scaffold并生成了名为“pictures”的模型,并在其上添加了paperclip附件。 模型和控制器(接收请求)如下所示。
模型
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
收到请求时,“rails server”控制台会说出类似的内容
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)
如您所见,只发送了一个参数,并且未收到“picture [image]”参数,即原始图像数据。 谁能帮我?
对不起,这是我的愚蠢错误。 我应该做的
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"));
应该使用新文件而不是文件
使用Android异步Http客户端( http://loopj.com/android-async-http/ )时,我们不必关心MultipartEntity。 谢谢所有回答我问题的人!!!!
在Android AsyncHttpClient官方页面(http://loopj.com/android-async-http/)中有如下所示的描述
“没有额外第三方库的多部分文件上传”
在“使用RequestParams上传文件”部分,他们有一个示例代码来上传图像
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
这就是我做的,虽然没有用。 这有助于回答我的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.