Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中上载之前,仅显示特定文件(如PDF)_Javascript_Html - Fatal编程技术网

在javascript中上载之前,仅显示特定文件(如PDF)

在javascript中上载之前,仅显示特定文件(如PDF),javascript,html,Javascript,Html,我有一个只允许上传特定文件类型的场景,例如PDF。 我不想在表单提交时验证类型。在选择文件之前,我需要验证类型是否正确。请记住,HTML5不支持accept属性 我有两个选择 只能选择可接受的文件类型 如果可能,在选择文件后触发Javascript函数。与onclick相反 这是我在提交时尝试验证的方式: 文件上载: 函数文件_upload() { var imgpath=document.getElementById('word').value; 如果(imgpath==“”){ 提醒(“上

我有一个只允许上传特定文件类型的场景,例如PDF。 我不想在表单提交时验证类型。在选择文件之前,我需要验证类型是否正确。请记住,HTML5不支持
accept
属性

我有两个选择

  • 只能选择可接受的文件类型
  • 如果可能,在选择文件后触发Javascript函数。与onclick相反
  • 这是我在提交时尝试验证的方式:

    
    文件上载:
    函数文件_upload()
    {
    var imgpath=document.getElementById('word').value;
    如果(imgpath==“”){
    提醒(“上传您的word文件”);
    document.file.word.focus();
    返回false;
    }
    否则{
    //获取文件扩展名的代码。。
    var arr1=新数组;
    arr1=imgpath.split(\\”;
    var len=arr1.1长度;
    var img1=arr1[len-1];
    var filext=img1.substring(img1.lastIndexOf(“.”+1);
    //检查分机
    如果(filext==“doc”| filext==“docx”){
    警报(“文件已正确上载”)
    返回true;
    }
    否则{
    警报(“选择的文件格式无效”);
    document.form.word.focus();
    返回false;
    }
    }
    }