Thanks, Dmitry, I tried your JS code suggestion, but its throwing error like :
Error: undefined local variable or method `obj' for #
Trace:
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rho/render.rb:118:in `getBinding'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rhoframework.rb:56:in `eval'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rhoframework.rb:56:in `eval_compiled_file'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rho/render.rb:175:in `render'
F:/tauimgupload/app/Image/image_controller.rb:10:in `index'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rho/rhocontroller.rb:101:in `serve'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rho/rhoapplication.rb:217:in `serve'
e:/RhomobileSuite7.2.2/ruby/lib/ruby/gems/2.3.0/gems/rhodes-7.2.2/lib/framework/rho/rho.rb:836:in `serve'
Issue is how to pass the selected image to the JS code when I click on "Upload with JS" in my below code :
<script type="text/javascript">
// Upload file
function uploadFile() {
//var files = document.getElementById("filename").files;
//var files = RhoApplication.get_blob_path(@image.image_uri);
//var files = Rho.Application.publicFolder+"/images/DOG.jpg";
var files = "<%= obj.image_uri %>";
//var files = '/app/db/db-files/Image_05-27-2022_05.07.08_-000.jpg';
alert(files);
if(files.length > 0 ){
var formData = new FormData();
formData.append("file", files[0]);
var xhttp = new XMLHttpRequest();
// Set POST method and ajax file path
xhttp.open("POST", "http://10.179.22.89/demotest.php", true);
// call on request changes state
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = this.responseText;
if(response == 1){
alert("Upload successfully.");
}else{
alert("File not uploaded.");
}
}
};
// Send request with data
xhttp.send(formData);
}else{
alert("Please select a file");
}
}
</script>
<div data-role="page">
<div data-role="header" data-position="inline">
<h1>TAU Pictures</h1>
<a href="<%= Rho::Application.startURI %>" class="ui-btn-left" data-icon="home" data-direction="reverse" <%= "data-ajax='false'" if is_bb6 %>>
Home
</a>
<a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">
New
</a>
</div>
<div data-role="content">
<ul data-role="listview">
<li>
<a href="<%= url_for(:action => :edit)%>">Choose picture</a>
</li>
<% if System::get_property('has_camera') %>
<li>
<a href="<%= url_for(:action => :new)%>">Take picture</a>
</li>
<% end %>
<% @images.each do |obj| %>
<li>
<img src="<%= obj.image_uri %>" width="300px"></img>
</li>
<li>
Location:<%=obj.image_uri%>
<%@@img_path=obj.image_uri%>
</li>
<li>
<a href="<%=url_for(:action => :delete, :id => obj.object)%>">Delete</a>
</li>
<li>
<a href="<%=url_for(:action => :upload_file, :id => obj.object)%>">Upload with Ruby</a>
</li>
<li>
<input type="button" id="btn_uploadfile" value="Upload with JS" onclick="uploadFile();" >
</li>
<% end %>
</ul>
</div>
</div>
I would have preferred Rho's Ruby or JS way of uploading, but those also do not work (see upload_file method below) with my controller code :
require 'rho/rhocontroller'
require 'helpers/browser_helper'
class ImageController < Rho::RhoController
include BrowserHelper
# GET /Image
def index
@images = Image.find(:all)
render :back => '/app'
end
def upload_file
@image = Image.find(@params['id'])
#@realpath=@params["imageUri"]
#blob_path = @params['id'].to_s
realpath = Rho::RhoApplication::get_blob_path(@image.image_uri)
#realpath = Rho::Application.expandDatabaseBlobFilePath(@image.image_uri)
#realpath = Rho::Application.relativeDatabaseBlobFilePath(@image.image_uri)
# #realpath = Rho::Application.publicFolder+"/images/DOG.jpg"
puts 'realpath=' + realpath
Alert.show_popup(realpath)
if Rho::Network.hasNetwork
#Perform an HTTP GET request.
netProps = Hash.new
#netProps['url'] = "http://10.179.22.241:8080/egranth/test"
netProps['url'] = "http://10.179.22.89/demotest.php"
response=Rho::Network.get(netProps)
if response['status']!='ok'
Rho::WebView.executeJavascript("alert('No data connection or Webservice is down');")
Rho::WebView.navigate(url_for(:action=>:index))
end
@parsed=response['status']
if @parsed
#Rho::WebView.executeJavascript("alert('Parsed!');")
#Alert.show_popup(@parsed)
puts "parsed: "+@parsed
## # Upload the specified file using HTTP POST.
uploadfileProps = Hash.new
#uploadfileProps['url'] = "http://10.179.22.241:8080/egranth/uploadBookImg"
uploadfileProps['url'] = "http://10.179.22.89/demotest.php"
uploadfileProps['body'] = "Uploading file"
#uploadfileProps['filename'] = Rho::Application.publicFolder+"/images/DOG.jpg"
uploadfileProps['file']=Rho::RhoApplication::get_blob_path(@image.image_uri)
#message = Rho::Application.publicFolder+"/images/DOG.jpg"
Alert.show_popup(uploadfileProps['file'])
uploadfileProps['fileContentType'] = "image/jpg"
#Alert.show_popup(uploadfileProps)
Rho::Network.uploadFile(uploadfileProps, url_for(:action => :upload_file_callback))
# WebView.execute_js('upload_file')
# Rho::WebView.navigate(url_for(:action=>:index))
end # @parsed
else
Rho::WebView.executeJavascript("alert('Network is not available');")
Rho::WebView.navigate(url_for(:action => :index))
end # has network
#Rho::Network.uploadFile(
#:url => "10.179.22.241/upload_test/upload.php",
#:url => "http://10.179.22.89/upload_test/upload2.php?filename="+Rho::Application.publicFolder+"/images/DOG.jpg"
#:url => "http://10.179.22.241:8080/egranth/uploadBookImg?filename="+Rho::Application.publicFolder+"/images/backButton.png"
# :multipart => [
# {
# :filename => Rho::Application.publicFolder+"/images/DOG.jpg",
# # if missed base name from file path used
# #:filename_base => Rho::Application.publicFolder+"/images/DOG.jpg",
# #:name => "image",
# :content_type => "application/octet-stream"
# },
# # You can specify file content inline.
# {
# :body => "upload test",
# :name => "upload_body_test",
# :content_type => "image/jpg"
# }
# ]
# )
### # Upload the specified file using HTTP POST.
# uploadfileProps = Hash.new
# uploadfileProps['url'] = "http://10.179.22.241:8080/egranth/uploadBookImg"
# uploadfileProps['file'] = realpath
# uploadfileProps['body'] = "uploading file"
# uploadfileProps['fileContentType']="image/jpg"
# Get and show the various app folders
# appFolder = Rho::Application.appBundleFolder
# appsBundleFolder = Rho::Application.appsBundleFolder
# databaseBlobFolder = Rho::Application.databaseBlobFolder
# publicFolder = Rho::Application.publicFolder
# userFolder = Rho::Application.userFolder
#
# message = "App bundle folder: "+appFolder+"\n"+
# "Apps bundle folder: "+appsBundleFolder+"\n"+
# "Database blob folder: "+databaseBlobFolder+"\n"+
# "Public folder: "+publicFolder+"\n"+
# "User folder: "+userFolder
# Alert.show_popup(message)
# Rho::Network.uploadFile(uploadfileProps, url_for(:action => :upload_file_callback))
end
def upload_file_callback
puts "upload_file_callback params: #{@params}"
if @params['status'] == "ok"
Alert.show_popup "Upload Succeeded."
WebView.navigate( url_for :action => :index )
else
Alert.show_popup "Upload Failed."
WebView.navigate( url_for :action => :index )
end
end
def new
width =100
height =100
settings = {:desired_width => width, :desired_height => height }
Camera::take_picture(url_for(:action => :camera_callback), settings)
""
end
# def new
# Camera::take_picture(url_for(:action => :camera_callback))
# #Rho::Camera.takePicture(url_for( :action => :picture_taken_callback))
# redirect :action => :index
#
# end
def picture_taken_callback
if (@params["status"]=="ok")
Alert.show_popup(@params.imageUri)
end
end
def edit
width =100
height =100
settings = {:desired_width => width, :desired_height => height }
Camera::choose_picture(url_for(:action => :camera_callback), settings)
end
# def edit
# #Camera::choose_picture(url_for :action => :camera_callback)
# Rho::Camera.choosePicture({}, url_for(:action => :camera_callback))
# redirect :action => :index
# end
def delete
@image = Image.find(@params['id'])
@image.destroy if @image
redirect :action => :index
end
def camera_callback
if @params['status'] == 'ok'
#imagebase64='data:image/jpeg;base64,'+Base64.encode64(open(Rho::RhoApplication.get_blob_path(@params['image_uri'])) { |io| io.read }).gsub(/\r/,"").gsub(/\n/,"")
#image = Image.new({'image_uri' => @params['image_uri'], 'image_base64' => imagebase64})
image = Image.new({'image_uri' => @params['image_uri']})
image.save
end
WebView.navigate(url_for(:action => :index ))
end
# def camera_callback
# if @params['status'] == 'ok'
# image = Image.new({'image_uri' => @params['image_uri']})
# image.save
# end
#
# WebView.navigate( url_for :action => :index )
# end
end
My model has been defined as a blob :
# The model has already been created by the framework, and extends Rhom::RhomObject
# You can add more methods here
class Image
include Rhom::PropertyBag
# Uncomment the following line to enable sync with Image.
#enable :sync
#add model specific code here
property :image_uri, :blob
end
Waiting for suggestions. Thanks