Post by egghead on Jun 16, 2022 12:13:14 GMT
Needed to upload files to a webservice that accepts file uploads and which is running in our local LAN. Webservice is written in PHP and tested with Postman.
However, my code does not upload the file(s). Intention is to take/choose picture using mobile device and then upload it. However, for testing, I have just hardcoded the path to the image file (realpath = Rho::Application.publicFolder+"/images/DOG.jpg" in code below). My code snippet :
def upload_file
@image = Image.find(@params['id'])
#realpath = Rho::RhoApplication::get_blob_path(@image.image_uri)
realpath = Rho::Application.publicFolder+"/images/DOG.jpg"
puts 'realpath=' + realpath
if Rho::Network.hasNetwork
#Perform an HTTP GET request.
netProps = Hash.new
netProps['url'] = "http://10.179.22.89/test.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['body']
if @parsed
Rho::WebView.executeJavascript("alert('Parsed!');")
puts "parsed: "+@parsed
## # Upload the specified file using HTTP POST.
uploadfileProps = Hash.new
uploadfileProps['url'] = "http://10.179.22.89/test.php"
uploadfileProps['filename'] = realpath
uploadfileProps['fileContentType'] = "image/jpg"
Rho::Network.uploadFile(uploadfileProps, url_for(:action => :upload_file_callback))
end # @parsed
else
Rho::WebView.executeJavascript("alert('Network is not available');")
Rho::WebView.navigate(url_for(:action => :index))
end # has network
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
My log (with MinSeverity = 0) is attached
The webservice response [parsed: "The file could not be uploaded to upload\/185"] in the log doesn't mention the file name after the word file which, according to the webservice author indicates that no file was detected.
Also, as shown in the log, log.txt (278.54 KB)
upload_file_callback params: {"rho_callback"=>"1", "body"=>"\"The file could not be uploaded to upload\\/72\"", "http_error"=>"200", "status"=>"ok", "headers"=>{"connection"=>"Keep-Alive", "content-length"=>"47", "content-type"=>"text/html", "date"=>"Thu, 16 Jun 2022 11:11:06 GMT", "keep-alive"=>"timeout=15, max=99", "server"=>"Apache/2.2.14 (Ubuntu)", "vary"=>"Accept-Encoding", "x-powered-by"=>"PHP/5.3.2-1ubuntu4.30"}}
does not seem to show any error.
Are there more ways to check the cause? Thanks
However, my code does not upload the file(s). Intention is to take/choose picture using mobile device and then upload it. However, for testing, I have just hardcoded the path to the image file (realpath = Rho::Application.publicFolder+"/images/DOG.jpg" in code below). My code snippet :
def upload_file
@image = Image.find(@params['id'])
#realpath = Rho::RhoApplication::get_blob_path(@image.image_uri)
realpath = Rho::Application.publicFolder+"/images/DOG.jpg"
puts 'realpath=' + realpath
if Rho::Network.hasNetwork
#Perform an HTTP GET request.
netProps = Hash.new
netProps['url'] = "http://10.179.22.89/test.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['body']
if @parsed
Rho::WebView.executeJavascript("alert('Parsed!');")
puts "parsed: "+@parsed
## # Upload the specified file using HTTP POST.
uploadfileProps = Hash.new
uploadfileProps['url'] = "http://10.179.22.89/test.php"
uploadfileProps['filename'] = realpath
uploadfileProps['fileContentType'] = "image/jpg"
Rho::Network.uploadFile(uploadfileProps, url_for(:action => :upload_file_callback))
end # @parsed
else
Rho::WebView.executeJavascript("alert('Network is not available');")
Rho::WebView.navigate(url_for(:action => :index))
end # has network
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
My log (with MinSeverity = 0) is attached
The webservice response [parsed: "The file could not be uploaded to upload\/185"] in the log doesn't mention the file name after the word file which, according to the webservice author indicates that no file was detected.
Also, as shown in the log, log.txt (278.54 KB)
upload_file_callback params: {"rho_callback"=>"1", "body"=>"\"The file could not be uploaded to upload\\/72\"", "http_error"=>"200", "status"=>"ok", "headers"=>{"connection"=>"Keep-Alive", "content-length"=>"47", "content-type"=>"text/html", "date"=>"Thu, 16 Jun 2022 11:11:06 GMT", "keep-alive"=>"timeout=15, max=99", "server"=>"Apache/2.2.14 (Ubuntu)", "vary"=>"Accept-Encoding", "x-powered-by"=>"PHP/5.3.2-1ubuntu4.30"}}
does not seem to show any error.
Are there more ways to check the cause? Thanks