Nw.js: nwworkingdir is not working on Windows

Created on 14 Apr 2015  路  15Comments  路  Source: nwjs/nw.js

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

needinfo

Most helpful comment

Hey, all! After a LOT of trial and error, I stumbled across a scenario for me that works. It seems, on Windows, setting other attributes essentially causes nwworkingdir to fail, or setting it as an attribute later causes it to fail (or something weird along those lines). I could get it to work in the developer tools console with just nwworkingdir, but then if I added other attributes, it would fail.

In other words, this worked:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" />').click()

But this didn't:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" nwsaveas="untitled" />').click()

Buuuuut... I realized that a "saveas" path doesn't have to be relative, therefore I tried:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" nwsaveas="C:\\Users\Matthew\\untitled" />').click()

This worked!

I used jQuery to build it, so it's not the same as these examples, but this is my final working code on Windows:

var input = '<input type="file" ';

            var fullPath = F.fullPath(options.path);

            if(options.type) {
                if(options.type === 'saveas') {
                    input+='nwsaveas="' + fullPath + Path.sep + options.name + '" ';
                }
                else if(options.type === 'openDirectory') {
                    input+='nwdirectory ';
                }   
            }
            input+='nwworkingdir="' + fullPath + '" />';

            var $dialog = $(input);

            $dialog.on('change', function() {
                callback.call(this);
                $dialog = null;

            });
            $dialog.click();

Hope that helps.

(End note: it would be nice to finally have a real File API for this, instead of this insanity. I'm not sure on what planet this is supposed to be friendly to developers.)

All 15 comments

Can you post the results from devtools:

process.versions
process.arch
process.platform

untitled

I've tried the below element and the value is set as "C:\Windows" thru the javascript.

<input name='file' type="file"/>
inputEl.nwworkingdir = 'C:\\Windows';

It would be helpful if you could tell how you're using the nwworkingdir property.
Is it intermittent? Because, it didn't work one time out of several tries.

Yeah, I stripped the code from the tests.

Seem #2948 seems the same problem. I am also still waiting for a bugfix.

Hey, all! After a LOT of trial and error, I stumbled across a scenario for me that works. It seems, on Windows, setting other attributes essentially causes nwworkingdir to fail, or setting it as an attribute later causes it to fail (or something weird along those lines). I could get it to work in the developer tools console with just nwworkingdir, but then if I added other attributes, it would fail.

In other words, this worked:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" />').click()

But this didn't:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" nwsaveas="untitled" />').click()

Buuuuut... I realized that a "saveas" path doesn't have to be relative, therefore I tried:
$('<input type="file" nwworkingdir="C:\\Users\Matthew\\" nwsaveas="C:\\Users\Matthew\\untitled" />').click()

This worked!

I used jQuery to build it, so it's not the same as these examples, but this is my final working code on Windows:

var input = '<input type="file" ';

            var fullPath = F.fullPath(options.path);

            if(options.type) {
                if(options.type === 'saveas') {
                    input+='nwsaveas="' + fullPath + Path.sep + options.name + '" ';
                }
                else if(options.type === 'openDirectory') {
                    input+='nwdirectory ';
                }   
            }
            input+='nwworkingdir="' + fullPath + '" />';

            var $dialog = $(input);

            $dialog.on('change', function() {
                callback.call(this);
                $dialog = null;

            });
            $dialog.click();

Hope that helps.

(End note: it would be nice to finally have a real File API for this, instead of this insanity. I'm not sure on what planet this is supposed to be friendly to developers.)

@matthew-dean how could you find that. You made my day!

@fritx In web developer years, I'm about 1,000 years old, lol. So it's just lots of experience problem-solving. And: no problem! Hope it helped you.

input.setAttribute('nwworkingdir', 'C:\Windows') // wont work
input.setAttribute('nwsaveas', 'filename')
const fullPath = join(dir, filename)
const input = document.createElement('input')
input.setAttribute('type', 'file')
input.setAttribute('nwsaveas', fullPath) // absolute path works
input.addEventListener('change', ({ target }) => {
  // target.files[0].path
  saveTo(target.value)
})
input.click()

The above solution worked fine in nw 0.12.3, however, it seems to remember last chosen savepath everytime ignoring nwsaveas after upgraded to nw18.

In nw18, the a filename (not fullPath) should be provided to nwsaveas.

@fritx Are you saying that setting the full path to nwsaveas _breaks_ in nw18? If so, it should be filed to a new bug.

Yes, last time I've tried it both in Mac 10.11 and Windows 7.
It seems like what I said.

nwworkingdir works fine for me on Windows with nwjs-sdk-v0.23.5.
<input type="file" nwworkingdir="/home/path/">

close until there is more information.

Just for your info. We're running nwjs-sdk-v0.29.0 on Windows 10 and it's working.

Similar issue: nwsaveas and nwworkingdir not working. Windows 7, 10, nwjs 0.37.3 normal and SDK versions.

nwworkingdir works well with nwjs-sdk-win-v0.37.3, and nwsaveas with file name doesn't work, but it works well with v0.36.5.

<input type="file" nwsaveas="setuperr.log" nwworkingdir="C:\Windows\debug" />

Was this page helpful?
0 / 5 - 0 ratings