2012-09-03 102 views
12

我試圖通過http post方法從android設備上傳圖像文件到rails服務器。 但張貼圖像無法正常工作。更具體地說,post參數(包括圖像文件)似乎沒有被正確發送。從Android(使用Android異步Http客戶端)上傳圖像到rails服務器(使用回形針)

我正在使用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!!!!"); 
     }                                          
    }); 
} 

至於軌回形針應用程序,我只是用腳手架和生成的模型命名爲「照片」並在其上添加回形針附加文件。模型和控制器(接收請求)如下所示。

模型

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並且未接收到作爲原始圖像數據的「圖像[圖像]」參數。誰能幫我?

+1

謝謝。我正在尋找類似的問題。 –

+0

我得到{ 「地位」: 「錯誤」, 「錯誤」:{ 「image_content_type」: 「無效」], 「形象」: 「無效」]}}。有人有過這個問題嗎? –

回答

12

對不起,那是我愚蠢的錯誤。 我應該做

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。 謝謝所有回答我的問題的人!

+0

我在我的應用程序中編寫了相同的代碼,但多次服務器給我錯誤,因爲圖像大小很大,您知道任何方式將大圖像發送到服務器 –

1

您可以通過MultipartEntity上傳圖像。

MultipartEntity,HttpMime 4.0及更高版本的一部分。允許您將多個由邊界字符串分隔並使用給定字符集編碼的部分放入到httppost請求中。

欲瞭解更多信息以及如何使用Multipart,請參閱thisthis

+0

請參閱我在答案中添加的其他鏈接。 – Shrikant

+0

非常感謝您的回答! 現在,我明白了使用MultipartEntity,通過instanciating MultipartEntity並把它應用addPart方法,然後調用setEntity方法HttpPost實例。但我使用的不是HttpPost類,而是AsyncHttpClient類。你能告訴我如何在AsyncHttpClient上做同樣的事嗎? – Ryo

+0

Ahhh ...對不起,你知道如何在AsyncHttpClient上使用MultipartEntity嗎? – Ryo

0

有像下面的說明,在Android的官方AsyncHttpClient頁面(http://loopj.com/android-async-http/)

「multipart文件,沒有額外的第三方庫」

而在「上傳文件與RequestParams」的部分,他們有一個示例代碼,上傳圖片

File myFile = new File("/path/to/file.png"); 
RequestParams params = new RequestParams(); 
try { 
    params.put("profile_picture", myFile); 
} catch(FileNotFoundException e) {} 

這是我做的,但沒有奏效。這有助於回答我的問題嗎?

+1

嗨Ryo,你得到了你的問題的答案? –

相關問題