我如何从Rhomobile上传图像

时间:2013-09-16 02:59:45

标签: rhomobile rhodes

我需要将用户个人资料图片上传到服务器,我将其存储在应用的基本路径上。

settings = { :camera_type => @params['camera_type'], 
             :color_model => @params['color_model'], 
             :enable_editing => en_ed, 
             :desired_width => width, 
             :desired_height => height, 
             :flash_mode => 'auto',
             :saveToDeviceGallery => 'true'}
Camera::take_picture(url_for(:action => :camera_callback), settings)

然后在回调中,

  Image.create({:id => generateId(), :image_uri => @params['image_uri']})

  file_testname = File.join(Rho::RhoApplication::get_blob_path(@params['image_uri']))
  test_content = File.binread(file_testname)
  new_file_name = File.join(Rho::RhoApplication::get_base_app_path(), @params['image_uri'].to_s().split("/")[2])
  f = File.new(new_file_name, "wb")
  f.write(test_content)
  f.close

如何将该图像上传到服务器?

1 个答案:

答案 0 :(得分:2)

您可以使用upload_file API

new_file_name = File.join(Rho::RhoApplication::get_base_app_path(), image.filename)
if File.exist?(new_file_name)

  # Upload the file
  result = Rho::AsyncHttp.upload_file(
    :url => "#{@@url}/image/upload",
    :filename => new_file_name,
    :headers => {},
    :body => ""
  )

  if result["status"].upcase == "OK"
    Alert.show_popup "Success"
  end

end

您需要将image.filename替换为您的路径。