control the working directory for <input type=“file”>?

前端 未结 6 1510
梦谈多话
梦谈多话 2020-11-29 11:11

Full story: I am working on a project for my company that requires users at each of our locations to upload reports generated by a third party software. All reports generate

相关标签:
6条回答
  • 2020-11-29 11:25

    This works. Here I needed to make sure the user has used input image file on the clients local image path. The UrlExists simple checks if the file could be loaded to the known path + file name. I also needed to make the input look better than the browsers. The trick here was placing I canvas with more work than shown here to mask the input. Setting input opacity to 0 , Here set to 0.5 so you can see the trick.

        <body>
        <canvas id="FancyCanvasButton" tabindex="8" width="240" height="18" style="position: absolute; left: 100px; top: 120px; border: 1px solid rgb(0, 0, 0); z-index: 1;"></canvas>
        <input type="file" accept="image/*" onchange="loadFile(event)" style="opacity:0.5;position: absolute; left: 100px; top: 120px; z-index: 2; " >
        <img id="output"/>
     <script>
     var loadFile = function(event) {
       var output = document.getElementById('output');
       var fName= "Images/" + event.target.files[0].name;  //None path with users selected file name
       if(UrlExists(fName)){
         output.src=fName;         //Do something like show image
       }else{
        alert("File not present"); // Do something to show file not in this path
       }
     };
    
     function UrlExists(url) {
        var http = new XMLHttpRequest();
        http.open('HEAD', url, false);
        http.send();
        if (http.status != 404){
           return true;
         }else{
           return false;
         }
      }
    
      </script>
    
    0 讨论(0)
  • 2020-11-29 11:27

    A quick Google search resulted in JUpload -- an open source signed Java applet that seems to meet all your needs: http://jupload.sourceforge.net/

    Demo site: http://jupload.sourceforge.net/advanced_js_demo.html

    I'm sure there are many other such Java applets out there and it probably wouldn't be too hard to roll your own.

    The only other option I can think of is a desktop uploading application.

    0 讨论(0)
  • 2020-11-29 11:32

    Simply put, it is impossible to do any such thing. As you can see, all/most of the answers agree with this statement, and those that do not misunderstood the question. Sorry, but what you are trying to do is considered a security risk by most browsers.

    0 讨论(0)
  • 2020-11-29 11:40

    You cannot control the contents of a file input using JavaScript for security reasons. Otherwise, you could create a hidden form with a file input field, set it to a path, and submit the form with JavaScript in order to upload the file to your server without the user knowledge.

    Unfortunately, I'm not aware of a way to set the default path for the file selector. If there is one, I'm sure it will be browser-specific and can only be used by setting some option in the client side, not through HTML or JavaScript.

    0 讨论(0)
  • 2020-11-29 11:43

    IE uses the last folder choosen for file uploads. If you have control over the client computers, my tips is; map a drive letter to the folder where the files are located. This can easliy be done using a cmd-file put in autoload calling subst. Then instruct the users to manually enter the drive letter, as it's very short it should be possible to write a very clear and easy to follow instruction. Good luck.

    0 讨论(0)
  • 2020-11-29 11:43

    You cannot set the value of an input file, that is a security issue.

    You can use an activex control, although that stuff is getting pretty out dated. There are many advanced file upload activex controls and I bet you can pick one up pretty cheap.

    Registry setting for Download directory might do the trick: (even though its an upload) and you would have to figure out how to set it every time they open IE

    HKCU\Software\Microsoft\Internet Explorer\DownloadDirectory
    
    0 讨论(0)
提交回复
热议问题