【问题标题】:jQuery: get the file name selected from <input type="file" />jQuery:获取从 <input type="file" /> 中选择的文件名
【发布时间】:2010-11-20 21:46:13
【问题描述】:

这段代码应该在 IE 中运行(甚至不要在 Firefox 中测试它),但它不能。 我想要的是显示附件的名称。有什么帮助吗?

<html>
<head>
  <title>example</title>    
  <script type="text/javascript" src="../js/jquery.js"></script>
  <script type="text/javascript">  
        $(document).ready( function(){            
      $("#attach").after("<input id='fakeAttach' type='button' value='attach a file' />");      
      $("#fakeAttach").click(function() {            
        $("#attach").click();        
        $("#maxSize").after("<div id='temporary'><span id='attachedFile'></span><input id='remove' type='button' value='remove' /></div>");        
        $('#attach').change(function(){
          $("#fakeAttach").attr("disabled","disabled");          
          $("#attachedFile").html($(this).val());
        });        
        $("#remove").click(function(e){
          e.preventDefault();
          $("#attach").replaceWith($("#attach").clone());
          $("#fakeAttach").attr("disabled","");
          $("#temporary").remove();
        });
      })
    }); 
  </script> 
</head>
<body>
  <input id="attach" type="file" /><span id="maxSize">(less than 1MB)</span>    
</body>
</html>

【问题讨论】:

  • 检查我的答案,我相信这是实现这一目标的最佳和最容易理解的方式。

标签: jquery events copy file-io


【解决方案1】:

就是这么简单:

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

无论如何,我建议使用名称或 ID 属性来选择您的输入。 加上事件,它应该是这样的:

$('input[type=file]').change(function(e){
  $in=$(this);
  $in.next().html($in.val());
});

【讨论】:

  • 谢谢!这适用于我提出的前一个(和简化的)问题,但不适用于扩展版本。
  • 如何获取文件的完整路径以上代码只给出了文件名。
  • @PHP 牧师,你不能。浏览器不会公开此类信息。
  • @PHP Priest,如果你能做到这一点,你就可以通过 AJAX 偷偷地传输有关用户文件系统的数据,而用户可能永远不会知道。想象一下,如果您可以以编程方式设置文件输入的值,情况会变得多么糟糕。是的。安全问题。
  • 这似乎有点过头了?我列出的方式更容易理解。
【解决方案2】:

最简单的方法是简单地使用下面这行jquery,使用这个你不会得到/fakepath的废话,你直接得到上传的文件:

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

其他一些有用的命令是:

获取文件名:

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

获取文件类型:

如果我要上传 PNG,它将返回 image/png

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

要获取文件的大小(以字节为单位):

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

你也不必使用这些命令on('change',你可以随时获取值,例如你可能有一个文件上传,当用户点击upload时,你只需使用我列出的命令.

【讨论】:

  • 为什么我会收到错误无法读取未定义的属性“名称”?
【解决方案3】:
$('input[type=file]').change(function(e){
    $(this).parents('.parent-selector').find('.element-to-paste-filename').text(e.target.files[0].name);
});

如果使用.val(),此代码将不会在Google Chrome 中的文件名前显示C:\fakepath\。

【讨论】:

    【解决方案4】:
    <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>
    

    【讨论】:

    • 谢谢!这有助于获取上传文件的实际名称,而不是完整路径。
    【解决方案5】:

    我使用了以下工作正常。

    $('#fileAttached').attr('value', $('#attachment').val())
    

    【讨论】:

      【解决方案6】:
      //get file input
      var $el = $('input[type=file]');
      //set the next siblings (the span) text to the input value 
      $el.next().text( $el.val() );
      

      【讨论】:

        【解决方案7】:

        添加隐藏的重置按钮:

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

        点击重置按钮清除输入。

        $("#Reset1").click();
        

        【讨论】:

        • 你能点击隐藏按钮吗?
        【解决方案8】:
        <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>
        

        【讨论】:

        • 谢谢我正在寻找这个我知道这不是主题但这是我的答案你知道;)
        猜你喜欢
        • 2018-05-11
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        相关资源
        最近更新 更多