FileList在移动浏览器的兼容性

在做微信公众号网页开发,上传图片的时候不想放在表单里发送,获取控件的files[0]时返回空对象,
使用FormDatanew FormData(document.querySelector('#createForm'))也是空对象,
除了这两种,还有其他的方案吗。在Android6.0的手机和IOS 9/10上测试结果一样
代码

<form id="createForm" method="post" enctype="multipart/form-data">
    <input id="poster" name="poster" type="file" class="poster"/>
    <button id="uploadBtn" type="button">上传海报</button>
    <button id="createBtn" type="button">发布</button>
</form>
$('input[name="poster"]').on('change', function (e) {
    if (this.value === '') {
        $('#uploadBtn').text('未选择文件');
    }
    else {
        alert(JSON.stringify(e.target.files[0]));
    }
});
$('#createBtn').on('click', function () {
    var formData = new FormData(document.querySelector('#createForm'));
    alert(JSON.stringify(formData));
    }

结果都是弹出空对象{}

阅读 2.3k
2 个回答

FormData兼容性挺好的呀,手机端基本支持的,看看你的代码呢

$('input[name="poster"]').on('change', function (e) {
   console.log(e.target.files);
});

这里献个丑了,有关图片上传可参考我的博客链接

你用什么控件可以在手机上选择图片的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题