How to open a “Select folder” dialog from NodeJS (server-side, not browser)?












0
















Why would you want to show a file/folder dialog on the server-side?



I'm building a project that is intended to be ran locally (both the Node server-side part and client-side in the browser), where I'd like to be able to select a path, add it to some list or JSON file, and then maintain some projects in it (webpack'ing, read files, serve via express, etc).



Mostly just for personal use, for now anyways.




The reason I ask to do this via Node instead of the browser is so I can somehow get around the security implications in modern browsers that prevents, upon selecting a folder, from revealing the full local folder paths on the client-side (from an <input> tag).



Not only that, but I also:




  • DON'T need to upload any files, or

  • DON'T need the list of files contained in the selected folder.


I just need:




  • a way to pick a folder in a user-friendly manner, and...

  • submit it's path to the server

  • (or have the server prompt for it, and store it somewhere).




Take this input tag for example:



<input id="open-project" type="file" />



This will result this type of popup, which is great for digging into folders, pasting portions of paths to quickly navigate where you need, go to your Quick Access / Favorites, etc...



Shows the Open file dialog



But it's intended for selecting files, with no paths exposed, nothing useful to pass on to the server.



However...



If you switch it to this...



<input id="open-project" type="file" webkitdirectory directory />



You end up with this dreadful dialog box, which assumes you want to upload ALL THE FILES contained in the folder.



enter image description here



enter image description here




(yes, I actually have way more than 16 files in my Windows directory lol)






So it doesn't really look like <input> is the way to go.



Maybe there's an existing module that does this on the server-side? That way I could:




  • 'Invoke' it from the client-side, via AJAX for example

  • which would then trigger it on the server

  • and then show me the folder-select prompt


Or...



Make, a... tree-view in the browser... that communicates back-and-forth with the node side to dig down the local filesystem...



Any suggestions?










share|improve this question



























    0
















    Why would you want to show a file/folder dialog on the server-side?



    I'm building a project that is intended to be ran locally (both the Node server-side part and client-side in the browser), where I'd like to be able to select a path, add it to some list or JSON file, and then maintain some projects in it (webpack'ing, read files, serve via express, etc).



    Mostly just for personal use, for now anyways.




    The reason I ask to do this via Node instead of the browser is so I can somehow get around the security implications in modern browsers that prevents, upon selecting a folder, from revealing the full local folder paths on the client-side (from an <input> tag).



    Not only that, but I also:




    • DON'T need to upload any files, or

    • DON'T need the list of files contained in the selected folder.


    I just need:




    • a way to pick a folder in a user-friendly manner, and...

    • submit it's path to the server

    • (or have the server prompt for it, and store it somewhere).




    Take this input tag for example:



    <input id="open-project" type="file" />



    This will result this type of popup, which is great for digging into folders, pasting portions of paths to quickly navigate where you need, go to your Quick Access / Favorites, etc...



    Shows the Open file dialog



    But it's intended for selecting files, with no paths exposed, nothing useful to pass on to the server.



    However...



    If you switch it to this...



    <input id="open-project" type="file" webkitdirectory directory />



    You end up with this dreadful dialog box, which assumes you want to upload ALL THE FILES contained in the folder.



    enter image description here



    enter image description here




    (yes, I actually have way more than 16 files in my Windows directory lol)






    So it doesn't really look like <input> is the way to go.



    Maybe there's an existing module that does this on the server-side? That way I could:




    • 'Invoke' it from the client-side, via AJAX for example

    • which would then trigger it on the server

    • and then show me the folder-select prompt


    Or...



    Make, a... tree-view in the browser... that communicates back-and-forth with the node side to dig down the local filesystem...



    Any suggestions?










    share|improve this question

























      0












      0








      0









      Why would you want to show a file/folder dialog on the server-side?



      I'm building a project that is intended to be ran locally (both the Node server-side part and client-side in the browser), where I'd like to be able to select a path, add it to some list or JSON file, and then maintain some projects in it (webpack'ing, read files, serve via express, etc).



      Mostly just for personal use, for now anyways.




      The reason I ask to do this via Node instead of the browser is so I can somehow get around the security implications in modern browsers that prevents, upon selecting a folder, from revealing the full local folder paths on the client-side (from an <input> tag).



      Not only that, but I also:




      • DON'T need to upload any files, or

      • DON'T need the list of files contained in the selected folder.


      I just need:




      • a way to pick a folder in a user-friendly manner, and...

      • submit it's path to the server

      • (or have the server prompt for it, and store it somewhere).




      Take this input tag for example:



      <input id="open-project" type="file" />



      This will result this type of popup, which is great for digging into folders, pasting portions of paths to quickly navigate where you need, go to your Quick Access / Favorites, etc...



      Shows the Open file dialog



      But it's intended for selecting files, with no paths exposed, nothing useful to pass on to the server.



      However...



      If you switch it to this...



      <input id="open-project" type="file" webkitdirectory directory />



      You end up with this dreadful dialog box, which assumes you want to upload ALL THE FILES contained in the folder.



      enter image description here



      enter image description here




      (yes, I actually have way more than 16 files in my Windows directory lol)






      So it doesn't really look like <input> is the way to go.



      Maybe there's an existing module that does this on the server-side? That way I could:




      • 'Invoke' it from the client-side, via AJAX for example

      • which would then trigger it on the server

      • and then show me the folder-select prompt


      Or...



      Make, a... tree-view in the browser... that communicates back-and-forth with the node side to dig down the local filesystem...



      Any suggestions?










      share|improve this question















      Why would you want to show a file/folder dialog on the server-side?



      I'm building a project that is intended to be ran locally (both the Node server-side part and client-side in the browser), where I'd like to be able to select a path, add it to some list or JSON file, and then maintain some projects in it (webpack'ing, read files, serve via express, etc).



      Mostly just for personal use, for now anyways.




      The reason I ask to do this via Node instead of the browser is so I can somehow get around the security implications in modern browsers that prevents, upon selecting a folder, from revealing the full local folder paths on the client-side (from an <input> tag).



      Not only that, but I also:




      • DON'T need to upload any files, or

      • DON'T need the list of files contained in the selected folder.


      I just need:




      • a way to pick a folder in a user-friendly manner, and...

      • submit it's path to the server

      • (or have the server prompt for it, and store it somewhere).




      Take this input tag for example:



      <input id="open-project" type="file" />



      This will result this type of popup, which is great for digging into folders, pasting portions of paths to quickly navigate where you need, go to your Quick Access / Favorites, etc...



      Shows the Open file dialog



      But it's intended for selecting files, with no paths exposed, nothing useful to pass on to the server.



      However...



      If you switch it to this...



      <input id="open-project" type="file" webkitdirectory directory />



      You end up with this dreadful dialog box, which assumes you want to upload ALL THE FILES contained in the folder.



      enter image description here



      enter image description here




      (yes, I actually have way more than 16 files in my Windows directory lol)






      So it doesn't really look like <input> is the way to go.



      Maybe there's an existing module that does this on the server-side? That way I could:




      • 'Invoke' it from the client-side, via AJAX for example

      • which would then trigger it on the server

      • and then show me the folder-select prompt


      Or...



      Make, a... tree-view in the browser... that communicates back-and-forth with the node side to dig down the local filesystem...



      Any suggestions?







      javascript node.js windows path openfiledialog






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 2 '18 at 14:01









      bigpbigp

      1,83921946




      1,83921946
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I've accomplished this by spawning a child powershell process, and passing that value back up to the parent. This would only work on a Windows server, but something like this should work:



          let psScript = `
          Function Select-FolderDialog
          {
          param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

          [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
          Out-Null

          $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
          $objForm.Rootfolder = $RootFolder
          $objForm.Description = $Description
          $Show = $objForm.ShowDialog()
          If ($Show -eq "OK")
          {
          Return $objForm.SelectedPath
          }
          Else
          {
          Write-Error "Operation cancelled by user."
          }
          }

          $folder = Select-FolderDialog # the variable contains user folder selection
          write-host $folder
          `


          That's essentially the script that you need to prompt for folder location, then write it to the host (similar to a console.log)



          then you'd need to execute this script and handle the output:



          var spawn = require("child_process").spawn,child;
          child = spawn("powershell.exe",psScript);
          child.stdout.on("data",function(data){
          console.log("Powershell Data: " + data);
          });
          child.stderr.on("data",function(data){
          //this script block will get the output of the PS script
          console.log("Powershell Errors: " + data);
          });
          child.on("exit",function(){
          console.log("Powershell Script finished");
          });
          child.stdin.end(); //end input





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51655571%2fhow-to-open-a-select-folder-dialog-from-nodejs-server-side-not-browser%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I've accomplished this by spawning a child powershell process, and passing that value back up to the parent. This would only work on a Windows server, but something like this should work:



            let psScript = `
            Function Select-FolderDialog
            {
            param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

            [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
            Out-Null

            $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
            $objForm.Rootfolder = $RootFolder
            $objForm.Description = $Description
            $Show = $objForm.ShowDialog()
            If ($Show -eq "OK")
            {
            Return $objForm.SelectedPath
            }
            Else
            {
            Write-Error "Operation cancelled by user."
            }
            }

            $folder = Select-FolderDialog # the variable contains user folder selection
            write-host $folder
            `


            That's essentially the script that you need to prompt for folder location, then write it to the host (similar to a console.log)



            then you'd need to execute this script and handle the output:



            var spawn = require("child_process").spawn,child;
            child = spawn("powershell.exe",psScript);
            child.stdout.on("data",function(data){
            console.log("Powershell Data: " + data);
            });
            child.stderr.on("data",function(data){
            //this script block will get the output of the PS script
            console.log("Powershell Errors: " + data);
            });
            child.on("exit",function(){
            console.log("Powershell Script finished");
            });
            child.stdin.end(); //end input





            share|improve this answer




























              1














              I've accomplished this by spawning a child powershell process, and passing that value back up to the parent. This would only work on a Windows server, but something like this should work:



              let psScript = `
              Function Select-FolderDialog
              {
              param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

              [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
              Out-Null

              $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
              $objForm.Rootfolder = $RootFolder
              $objForm.Description = $Description
              $Show = $objForm.ShowDialog()
              If ($Show -eq "OK")
              {
              Return $objForm.SelectedPath
              }
              Else
              {
              Write-Error "Operation cancelled by user."
              }
              }

              $folder = Select-FolderDialog # the variable contains user folder selection
              write-host $folder
              `


              That's essentially the script that you need to prompt for folder location, then write it to the host (similar to a console.log)



              then you'd need to execute this script and handle the output:



              var spawn = require("child_process").spawn,child;
              child = spawn("powershell.exe",psScript);
              child.stdout.on("data",function(data){
              console.log("Powershell Data: " + data);
              });
              child.stderr.on("data",function(data){
              //this script block will get the output of the PS script
              console.log("Powershell Errors: " + data);
              });
              child.on("exit",function(){
              console.log("Powershell Script finished");
              });
              child.stdin.end(); //end input





              share|improve this answer


























                1












                1








                1







                I've accomplished this by spawning a child powershell process, and passing that value back up to the parent. This would only work on a Windows server, but something like this should work:



                let psScript = `
                Function Select-FolderDialog
                {
                param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

                [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
                Out-Null

                $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
                $objForm.Rootfolder = $RootFolder
                $objForm.Description = $Description
                $Show = $objForm.ShowDialog()
                If ($Show -eq "OK")
                {
                Return $objForm.SelectedPath
                }
                Else
                {
                Write-Error "Operation cancelled by user."
                }
                }

                $folder = Select-FolderDialog # the variable contains user folder selection
                write-host $folder
                `


                That's essentially the script that you need to prompt for folder location, then write it to the host (similar to a console.log)



                then you'd need to execute this script and handle the output:



                var spawn = require("child_process").spawn,child;
                child = spawn("powershell.exe",psScript);
                child.stdout.on("data",function(data){
                console.log("Powershell Data: " + data);
                });
                child.stderr.on("data",function(data){
                //this script block will get the output of the PS script
                console.log("Powershell Errors: " + data);
                });
                child.on("exit",function(){
                console.log("Powershell Script finished");
                });
                child.stdin.end(); //end input





                share|improve this answer













                I've accomplished this by spawning a child powershell process, and passing that value back up to the parent. This would only work on a Windows server, but something like this should work:



                let psScript = `
                Function Select-FolderDialog
                {
                param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

                [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
                Out-Null

                $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
                $objForm.Rootfolder = $RootFolder
                $objForm.Description = $Description
                $Show = $objForm.ShowDialog()
                If ($Show -eq "OK")
                {
                Return $objForm.SelectedPath
                }
                Else
                {
                Write-Error "Operation cancelled by user."
                }
                }

                $folder = Select-FolderDialog # the variable contains user folder selection
                write-host $folder
                `


                That's essentially the script that you need to prompt for folder location, then write it to the host (similar to a console.log)



                then you'd need to execute this script and handle the output:



                var spawn = require("child_process").spawn,child;
                child = spawn("powershell.exe",psScript);
                child.stdout.on("data",function(data){
                console.log("Powershell Data: " + data);
                });
                child.stderr.on("data",function(data){
                //this script block will get the output of the PS script
                console.log("Powershell Errors: " + data);
                });
                child.on("exit",function(){
                console.log("Powershell Script finished");
                });
                child.stdin.end(); //end input






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 2 '18 at 16:30









                Roger HernandezRoger Hernandez

                113




                113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51655571%2fhow-to-open-a-select-folder-dialog-from-nodejs-server-side-not-browser%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()