How to share file object from sketchup to javascript?


#1

Hi,
I want to upload a file - zip - to my server. File is created in ruby context.
Using javascript I want to upload the file .
How can I pass the file object reference to js ?

Passing the file path won’t work as js will not be able to load file in memory from the path.


#2

These are common questions asked on stackoverflow.com


#3

But ActiveXObject is only available of IE that too old version. It is not supported by mozilla or chrome.
So I cannot use it in Sketchup.


#4

What do you want to pass, a reference to the file (such as a path) or the actual file content? Is the “server” running locally as a part of a plugin or is it an actual remote (web) server? Will the user view a web page in a web/html dialog or is the communication taking place “under the hood” of the plugin with the user interacting to something else?

A good starting place might be the HTTP module of the Ruby API: http://ruby.sketchup.com/Sketchup/Http.html

A html file input could also work in some situations, it all depends on your use case.


#5

I want to upload a file using javascript to remote server.
File is nothing but the exported and zipped version of the model.

Plugin UI is shown using the HtmlDialog. Once user selects upload button I prepare the zip and wants to upload it through javascript. But javascript needs file reference to the zip .

How can I pass the reference to javascript from ruby for the zip file ?


#6

If the user is selected the file themselves, use a <input type=“file” /> element. Otherwise I wouldn’t do the upload through the web/HTML dialog at all. I don’t think Javascript can be used for this.


#7

<input type="file" ...> requires manual file selection by the user for security reasons.


There are other more complicated means such as AJAX.
These are covered in many threads at stackoverflow.com


You might try to send from the Ruby-side via a UI::HtmlDialog action callback, using Ruby’s Net::FTP library.

http://ruby-doc.org/stdlib-2.0.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-putbinaryfile

I’ve successfully downloaded zip files from internet servers using Net::FTP, in tests.


#8

I have tried with ruby. The problem with ruby is that it hangs Sketchup. Sketchup goes to not responding state.
That is why I was looking for a way to transfer through javascript.


#9

Did you try putting the Net::FTP call inside a single loop UI.start_timer block ?


#10

Let me try this.
I am using Amazon aws SDK to upload. Need to check if there is an API which sends the file in chunks so that I can use UI.start_timer to make it async.


#11

as your using Ruby 2.2, you may find Thread.new will do what you need in this situation…

john


#12

As far I remember Threads do not work in embedded ruby.
Even I tried some basic tests on threads in Sketchup console. It was not working as expected.
If you have working examples please share.


#13

SU modifies #puts which is why the simple Thread test’s fail…

If your not relying on writing to console or creating geometry in them, they should work…

john


#14

I tried this

Thread.new {
  # long waiting task
}.join

Still Sketchup went to not responding state.
I am missing something here ?


#15

Thomas might have written a method that on WIndows can cause the SketchUp application to peek it’s window messages at an interval that prevents the Not Responding state.
Check his TT_Lib2 code at BitBucket.
https://bitbucket.org/thomthom/


#16

When you call .join it will cause the main thread to wait for your thread to complete before it continues.
Additionally, Ruby threads doesn’t work well inside SketchUp. For an unknown reason they don’t appear to do work unless the main thread is busy. On top of that the Ruby API can only be called from the main thread.


#17

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.