How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

后端 未结 11 2599
谎友^
谎友^ 2020-11-21 04:36

How to get full path of file while selecting file using




        
相关标签:
11条回答
  • 2020-11-21 05:04

    Did you mean this?

    $('#i_file').change( function(event) {
    var tmppath = URL.createObjectURL(event.target.files[0]);
        $("img").fadeIn("fast").attr('src',tmppath);       
    });
    
    0 讨论(0)
  • 2020-11-21 05:04

    One interesting note: although this isn't available in on the web, if you're using JS in Electron then you can do this.

    Using the standard HTML5 file input, you'll receive an extra path property on selected files, containing the real file path.

    Full docs here: https://github.com/electron/electron/blob/master/docs/api/file-object.md

    0 讨论(0)
  • 2020-11-21 05:06

    For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:

    $('input[type=file]').change(function () {
        console.log(this.files[0].mozFullPath);
    });
    

    http://jsfiddle.net/SCK5A/

    So don't waste your time.

    edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.

    0 讨论(0)
  • 2020-11-21 05:11

    You can use the following code to get a working local URL for the uploaded file:

    <script type="text/javascript">    
        var path = (window.URL || window.webkitURL).createObjectURL(file);
        console.log('path', path);
    </script>
    
    0 讨论(0)
  • 2020-11-21 05:11

    You can get the full path of the selected file to upload only by IE11 and MS Edge.

    var fullPath = Request.Form.Files["myFile"].FileName;
    
    0 讨论(0)
  • 2020-11-21 05:15

    You can, if uploading an entire folder is an option for you

    <input type="file" webkitdirectory directory multiple/>
    

    change event will contain:

    .target.files[...].webkitRelativePath: "FOLDER/FILE.ext"
    
    0 讨论(0)
提交回复
热议问题