How to share file object from sketchup to javascript?

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.

These are common questions asked on stackoverflow.com

2 Likes

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.

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: Module: Sketchup::Http — SketchUp Ruby API Documentation

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

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 ?

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.

1 Like

<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.

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

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.

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

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.

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

john

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.

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

I tried this

Thread.new {
  # long waiting task
}.join

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

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/

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.

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