jQuery 中如何使用formdata 上传图片?

图片在上传过程中,需要将图片转换成 二进制数据,要怎么写呢?网上找的都不合适。我是这样写的,因为在上传前需要预览,也可以多图上传,下面的方法是获取不到图片数据的,只能获取到图片的name,type,size:


<div class="anviz-upload-file">

    <div class="image-item">

        <input id="file" type="file" />

    </div>

</div>

https://img4.mukewang.com/5c6e5aa10001b48312630853.jpg

慕尼黑的夜晚无繁华
浏览 386回答 1
1回答

慕姐4208626

直接push,有兴趣可以看看我的博客链接描述//onchange&nbsp;&nbsp; &nbsp; for(var i = 0;i<files.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fileArr.push(files[i]);&nbsp; &nbsp; &nbsp; &nbsp; //....图片的预览操作&nbsp; &nbsp; }//提交function sub(){&nbsp; &nbsp;&nbsp;var formData = new FormData();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for(var i =0;i<fileArr.length;i++){&nbsp; &nbsp; //提交时,我们把fileArr中的数据遍历一遍&nbsp; &nbsp; &nbsp; &nbsp; formData.append("upfile[]", fileArr[i]);&nbsp;&nbsp; &nbsp; };&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "1.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:formData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache:false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//不设置缓存&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; // 不处理数据&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: false&nbsp; &nbsp;// 不设置内容类型&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript