$('#profile_picture').on("change",function(){
var files = !!this.files ? this.files : [];
上面的代码!!this.files ? this.files : []
是什么意思?
if (!files.length || !window.FileReader) return;
if (/^image/.test( files[0].type)){
var reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onloadend = function(){
$("#imgchange").attr("src", "+this.result+");
}
}
});
答案 0 :(得分:0)
用于检查是否支持新的FileAPI。如果不支持新文件api,则文件输入的files
属性将不确定,因此!this.files
将为false,当我们再添加一个!
时,false将再次被否定为真
$('#profile_picture').on("change", function () {
// the files is a new property from the new File API, if if it is not supported assign an empty array as the value of files
var files = !! this.files ? this.files : [];
//if there are no files and FileReader is not supported return
if (!files.length || !window.FileReader) return;
if (/^image/.test(files[0].type)) {
var reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onloadend = function (event) {
$("#imgchange").prop("src", event.target.result);
}
}
});