jQuery: get the file name selected from <input type=“file” />

后端 未结 8 1258
情歌与酒
情歌与酒 2020-11-27 03:35

This code should work in IE (don\'t even test it in Firefox), but it doesn\'t. What I want is to display the name of the attached file. Any help?

         


        
相关标签:
8条回答
  • 2020-11-27 03:42
    <input onchange="readURL(this);"  type="file" name="userfile" />
    <img src="" id="blah"/>
    <script>
     function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
    
            reader.onload = function (e) {
                $('#blah')
                        .attr('src', e.target.result)
                        .width(150).height(200);
            };
    
            reader.readAsDataURL(input.files[0]);
            //console.log(reader);
            //alert(reader.readAsDataURL(input.files[0]));
        }
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 03:46

    Add a hidden reset button :

    <input id="Reset1" type="reset" value="reset" class="hidden" />
    

    Click the reset button to clear the input.

    $("#Reset1").click();
    
    0 讨论(0)
  • 2020-11-27 03:47

    The simplest way is to simply use the following line of jquery, using this you don't get the /fakepath nonsense, you straight up get the file that was uploaded:

    $('input[type=file]')[0].files[0]; // This gets the file
    $('#idOfFileUpload')[0].files[0]; // This gets the file with the specified id
    

    Some other useful commands are:

    To get the name of the file:

    $('input[type=file]')[0].files[0].name; // This gets the file name
    

    To get the type of the file:

    If I were to upload a PNG, it would return image/png

    $("#imgUpload")[0].files[0].type
    

    To get the size (in bytes) of the file:

    $("#imgUpload")[0].files[0].size
    

    Also you don't have to use these commands on('change', you can get the values at any time, for instance you may have a file upload and when the user clicks upload, you simply use the commands I listed.

    0 讨论(0)
  • 2020-11-27 03:47
    <input onchange="readURL(this);"  type="file" name="userfile" />
    <img src="" id="viewImage"/>
    <script>
     function readURL(fileName) {
        if (fileName.files && fileName.files[0]) {
            var reader = new FileReader();
    
            reader.onload = function (e) {
                $('#viewImage')
                        .attr('src', e.target.result)
                        .width(150).height(200);
            };
    
            reader.readAsDataURL(fileName.files[0]);
        }
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 03:49

    It is just such simple as writing:

    $('input[type=file]').val()
    

    Anyway, I suggest using name or ID attribute to select your input. And with event, it should look like this:

    $('input[type=file]').change(function(e){
      $in=$(this);
      $in.next().html($in.val());
    });
    
    0 讨论(0)
  • 2020-11-27 03:52

    I had used following which worked correctly.

    $('#fileAttached').attr('value', $('#attachment').val())
    
    0 讨论(0)
提交回复
热议问题