android/rails分段上传问题

发布于 2024-08-27 06:13:31 字数 1495 浏览 2 评论 0原文

我的问题是,我尝试将图像和一些文本值上传到 Rails 服务器,并且文本值最终作为文件,仅包含参数值。

帖子在

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>}}

Android 代码的服务器部分看起来如何

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 {

 }

My problem is that I try to upload an image and some text values to an rails server, and the text values end up as files, insted of just param values.

How the post looks on the server

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>}}

part of the android code

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 {

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-09-03 06:13:31

这正是 Rails 处理多部分表单数据的方式。

为了方便起见,我在 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 哈希。

That's just the way Rails does multipart form data.

For convenience I added this before_filter in my ApplicationController:

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

which parses each part into a JSON hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文