質問

私の問題は、私は、サーバーをレールに画像やテキスト値をアップロードしようとすると、テキスト値がちょうどのparam値のinsted、ファイルとして終わるということです。

どのようにサーバー上のポストに見える。

Parameters: {"action"=>"create", "controller"=>"problems",
"problem"=>{"lon"=>#File:/tmp/RackMultipart20100404-598-8pi1vj-0>, 
"photos_attributes"=>{"0"=>{"image"=>#File:/tmp/RackMultipart20100404-598-pak6jk-0>}}, 
"subject"=>#File:/tmp/RackMultipart20100404-598-nje11p-0>, 
"category_id"=>#File:/tmp/RackMultipart20100404-598-ijy1oo-0>, 
"lat"=>#File:/tmp/RackMultipart20100404-598-1a7140w-0>, 
"email"=>#File:/tmp/RackMultipart20100404-598-1b7w6jp-0>}}

アンドロイドコードの一部

try {
   File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg");

   HttpClient client = new DefaultHttpClient();  
   HttpPost post = new HttpPost("http://homepage.com/path");  
   FileBody bin = new FileBody(file);  


   Charset chars = Charset.forName("UTF-8");

   MultipartEntity reqEntity = new MultipartEntity();  
   reqEntity.addPart("problem[photos_attributes][0][image]", bin);  
   reqEntity.addPart("problem[category_id]", new StringBody("17", chars));

   post.setEntity(reqEntity); 
   HttpResponse response = client.execute(post);  

   HttpEntity resEntity = response.getEntity();  
   if (resEntity != null) {    
     resEntity.consumeContent();  
  }

   return true;

  } catch (Exception ex) {

    globalStatus = UPLOAD_ERROR;
    serverResponse = "";
    return false;
  } finally {

 }
役に立ちましたか?

解決

だけの方法です。マルチパートフォームデータを行いレールます。

利便性のために私は私のApplicationControllerにこのbefore_filterを追加します:

def read_and_parse_json_file_params
  file_params = params.select { |name,value| value.is_a?(File) || value.is_a?(Tempfile) }
  file_params.reject! { |name,file| file.content_type !~ %r{^application/json} }

  file_params.each do |name,file|
    file.rewind
    params[name] = ActiveSupport::JSON.decode(file.read)
  end
end
JSONハッシュに各部分を解析し、

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top