Jquery在文件类型输入中检查上传文件名 [英] Jquery checking the upload filename in file type input

查看:145
本文介绍了Jquery在文件类型输入中检查上传文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信读预范范范内范范中当信息范讯信息作方信范亦方方方亦作范范内亦范范亦作信息中内信息信息信息b

< input class =name =myfileid =myfiletype =filemultiple =falsedata-value =>

我的JQuery脚本:

$ p $函数checkFileName(filename, file_id){
var must_filename;信范范内信息范范亦范端范范亦范秘程预期
jQuery('。file_err_det')。remove();
alert(must_filename +'!='+ filename);
//文件名值始终为空?
if(must_filename!= filename){
alert('文件名无效!请按照格式重新上传');
jQuery(file_id).val('');


$ b jQuery('#myfile')。blur(function(){
var this_filename = jQuery(this).val()。split() '。')。slice(0,2).join('。');
checkFileName(this_filename,'#myfile');
alert(this_filename); // this returns null?I dont知道为什么..
});

但是看起来值 this_filename 是null 。我正在使用 blur ,是否有任何可以用来检查 jQuery('#myfile')。val() 使用File_from_web_applicationsrel =nofollow>使用来自web应用程序的文件


File API可以访问一个FileList,方态方范亦读据说亦读信息亦范范段中信息亦范端亦数信息中信息上范读范范中信息资范信息亦中信息亦读信息预秘上亦亦信息上范读中信息中信息内预辛> var selectedFile = document.getElementById('input')。files [0];



使用jQuery选择器访问一个选定的文件: / p>

var selectedFile = $('#input')。get(0).files [0]; p>

var selectedFile = $('#input')[0] .files [0];







尝试使用变更事件

 

($ change),function(e){var file = e.target.files [0];}。 console.log(file,file.name);})

< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script>< input class =name = myfileid =myfiletype =filemultiple =falsedata-value =>


I was trying to check the filename by the filename format given using JQuery..

this is my code:

<input class="" name="myfile" id="myfile" type="file" multiple="false" data-value="">

my JQuery Script:

function checkFileName(filename, file_id){
    var must_filename;

    must_filename = jQuery(file_id).attr('data-value').split('.').slice(0, 2).join('.');
    jQuery('.file_err_det').remove();
    alert(must_filename + ' != ' + filename);
    // filename value is always null?
    if(must_filename != filename){          
        alert('Filename is not valid! Please follow the format and upload it again');
        jQuery(file_id).val('');
    }
}

jQuery('#myfile').blur(function() {
    var this_filename = jQuery(this).val().split('.').slice(0, 2).join('.');
    checkFileName(this_filename, '#myfile');
    alert(this_filename); // this returns null? I dont know why..
});

but it seems the value this_filename is null. I am using blur, is there anything that I can use to check the jQuery('#myfile').val()?

解决方案

See Using files from web applications

The File API makes it possible to access a FileList containing File objects representing the files selected by the user.

If the user selects just one file, it is then only necessary to consider the first file of the list.

Accessing one selected file using a classical DOM selector:

var selectedFile = document.getElementById('input').files[0];

Accessing one selected file using a jQuery selector:

var selectedFile = $('#input').get(0).files[0];

var selectedFile = $('#input')[0].files[0];


Try utilizing change event

$("#myfile").on("change", function(e) {
  var file = e.target.files[0];
  console.log(file, file.name);
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<input class="" name="myfile" id="myfile" type="file" multiple="false" data-value="">

这篇关于Jquery在文件类型输入中检查上传文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆