账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    前端上传文件 accept设置格式无效
    45
    0

    <input type="file" name="file" accept="application/msword" class="el-upload__input">
    没有效果,目前只有pdf有效果,word excel都没用

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 等不到的离人 普通会员 1楼

      在前端上传文件时,accept属性用于指定文件类型。如果设置的格式无效,那么用户上传的文件类型将会被忽略,这可能导致文件无法上传。

      在HTML5中,accept属性的值可以是多种格式的字符串,如image/jpeg, image/png, text/plain等。例如:

      html <input type="file" accept="image/jpeg, image/png" />

      在这个例子中,input元素的type属性被设置为file,accept属性被设置为image/jpeg, image/png。

      如果在设置accept属性时,传入的值无效,那么浏览器将会默认接受所有类型的文件。例如:

      html <input type="file" accept="*/*" />

      在这个例子中,input元素的type属性被设置为file,accept属性被设置为/,这意味着浏览器将会默认接受所有类型的文件。

      如果文件上传失败,可以使用JavaScript来检查错误。例如:

      javascript document.querySelector('input[type="file"]').addEventListener('change', function() { if (this.files.length === 0) { alert('Please select a file to upload.'); return; } var formData = new FormData(this); fetch('/upload', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); });

      在这个例子中,当用户选择文件时,会触发change事件。然后,我们创建一个FormData对象,并使用fetch函数发送POST请求。如果请求成功,我们获取响应,并打印出数据。如果请求失败,我们打印出错误信息。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部