【问题标题】:check mimetype in javascript based on two condition根据两个条件检查 javascript 中的 mimetype
【发布时间】:2018-03-11 10:42:05
【问题描述】:

我在我的 javascript 文件中使用了一个函数,其中该函数被称为 onchange 事件。

function restrictUpload(event,fileID,path){
var files=event.target.files;
var mimeType=files[0].type;
 if(mimeType!="image/jpeg" || mimeType!="image/png"){
 console.log(mimeType);
 alert("Upload only JPEG,JPG,PNG");
 $('#'+fileID).val('');
 document.getElementById("userPhoto").src = path+"/default.png";
 }
}

当我上传 jpeg 图像时,它会显示警报,pdf 文件和 png 文件也会发出警报。我需要文件不是 jpeg 和 png 格式才能进入 if 条件。

【问题讨论】:

  • 您获得的每种 mime 类型将不同于 image/jpegimage/png(或两者)。你应该在那里调整你的状况。
  • 和 mimeType!=="image/jpeg"
  • @Sirko 如果我只设置一个条件,它就可以正常工作。喜欢if(mimeType!="image/jpeg"){ ... },但我也需要检查 png ..
  • @ChrisChen 也试过无济于事。任何其他方法..
  • 我可能会反其道而行之:if ( [ 'image/jpeg', 'image/png' ].includes( mimeType ) ) { ... } else { ... }。您还输出了 mimeType 本身 - 您是否检查过您获得的值是否符合您的预期?

标签: javascript


【解决方案1】:

根据 Sirko 的评论,我做了这个回答。

if (! [ 'image/jpeg', 'image/png' ].includes( mimeType ) )
  { ... }
else 
  { ... }

【讨论】:

    【解决方案2】:

    您不希望 mimeType 与 jpeg 和 png 不同吗?

    if(mimeType!="image/jpeg" && mimeType!="image/png"){
     ...
    }
    

    【讨论】:

    • 我一次只上传一个文件。你的也没用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多