Search code examples
rhomobilerhodes

How do i upload image from Rhomobile


I need to upload user profile image to server, i have it stored it on the app's base path.

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)

Then on callback,

  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

How can i upload that image to server?


Solution

  • You can use the 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
    

    You need to replace image.filename with your path.