FormData 对象上传二进制文件
XMLHttpRequest
发送请求的键/值对。它可以更灵活方便的发送表单数据,因为可以独立于表单使用。如果你把表单的编码类型设置为multipart/form-data ,则通过FormData传输的数据格式和表单通过submit() 方法传输的数据格式相同,也就是二进制文件。
{lastModified:1247549551674lastModifiedDate:Tue Jul 14 2009 13:32:31 GMT+0800 (中国标准时间) {}name:"ju.jpg"size:879394type:"image/jpeg"webkitRelativePath:"" }
可以自己创建一个FormData对象,然后通过调用它的append()方法添加字段,就像这样:
var formData = new FormData();formData.append("username", "Groucho"); formData.append("accountnum", 123456); // 数字 123456 会被立即转换成字符串 "123456"// HTML 文件类型input,由用户选择 formData.append("userfile", fileInputElement.files[0]);// JavaScript file-like 对象var content = '<a id="a"><b id="b">hey!</b></a>'; // 新文件的正文...var blob = new Blob([content], { type: "text/xml"}); formData.append("webmasterfile", blob);var request = new XMLHttpRequest(); request.open("POST", "http://foo.com/submitform.php"); request.send(formData);
通过表单创建 FormData 对象
<form id="uploadForm" enctype="multipart/form-data"><input id="file" type="file" name="file"/><button id="upload" type="button">upload</button> </form> enctype="multipart/form-data" 文件的二进制属性
<form id="uploadForm" enctype="multipart/form-data"><input id="file" type="file" name="file"/><button id="upload" type="button">upload</button> </form> $.ajax({url: '/upload',type: 'POST',cache: false,data: new FormData($('#uploadForm')[0]),processData: false,contentType: false }).done(function(res) { }).fail(function(res) {});
- processData设置为false。因为data值是FormData对象,不需要对数据做处理。
- <form>标签添加enctype="multipart/form-data"属性。
- cache设置为false,上传文件不需要缓存。
- contentType设置为false。因为是由<form>表单构造的FormData对象,且已经声明了属性enctype="multipart/form-data",所以这里设置为false。
<form>
表单构造FormData
对象,常用
<div id="uploadForm"><input id="file" type="file" multiple/><button id="upload" type="button">upload</button> </div>var formData = new FormData(); formData.append('file', $('#file')[0].files[0]); $.ajax({url: '/upload',type: 'POST',cache: false,data: formData,processData: false,contentType: false }).done(function(res) {// }).fail(function(res) {// });
- append()的第二个参数应是文件对象,即$('#file')[0].files[0]。
- contentType也要设置为‘false’。
Form Data 图片上传,手机版,使用 mui 的方法,效果图为:
<style>.anviz-upload-file .image-item{width: 30px;height: 30px;background-image: url(../../img/icon/add.png);background-size: 100% 100%;display: inline-block;position: relative;border-radius: 5px;margin-right: 10px;margin-bottom: 10px;border: solid 1px #e8e8e8;margin-left: 20px;}.anviz-upload-file .image-item .image-close{position: absolute;display: inline-block;right: -6px;top: -6px;width: 20px;height: 20px;text-align: center;line-height: 20px;border-radius: 12px;background-color: #FF5053;color: #f3f3f3;border: solid 1px #FF5053;font-size: 9px;font-weight: 200;z-index: 1;}.anviz-upload-file .image-item input[type="file"]{position: absolute;left: 0px;top: 0px;width: 100%;height: 100%;opacity: 0;cursor: pointer;z-index: 0;}.img-list{width: 100%;height: 105px;padding: 10px 10px;overflow: hidden;border-top: 1px solid #c8c7cc;border-bottom: 1px solid #c8c7cc;background: #fff;margin: 0;display: -webkit-box;display: -ms-flexbox;display: flex;justify-content: flex-start;overflow: scroll;}.img-list li{position: relative;margin-right: 15px;}.img-list li img{width: 85px;height: 85px;}.img-list li span{position: absolute;top: -5px;left: 73px;background: #00a0e8;width: 20px;height: 20px;border-radius: 20px;text-align: center;line-height: 18px;color: #fff;}</style><ul class="mui-table-view mui-grid-view mui-grid-9"><li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6 anviz-upload-warp" style="display: flex;padding: 0;"><h5 class="anviz-padded">Attachments</h5><div class="anviz-upload-file"><div class="image-item"><input id="file" type="file" /></div></div></li> </ul> <ul id="imgList" class="img-list"></ul>
<script>mui.ready(function(){var myfile = document.getElementById('file'); var List = document.getElementsByClassName('img-list')[0];myfile.onchange = function(){var files = this.files;if(!files)return; for(var i = 0;i<files.length;i++){ var oLi = '<li><img src="'+URL.createObjectURL(files[i])+'"><span class="close" οnclick="closeli(this)" >×</span></li>'; List.innerHTML+=oLi; }}});function closeli(obj){var filearr = [];var closes = document.getElementsByClassName('close');[].slice.call(closes).forEach(function(dom,index){if(obj === closes[index]){filearr.splice(index,1); };});obj.parentNode.remove(); }</script>
亲测可用!欢迎指正交流。
转载于:https://www.cnblogs.com/baiyygynui/p/8463771.html
FormData 对象上传二进制文件相关推荐
- JQuery Ajax使用FormData对象上传文件 图片
通过jQuery Ajax使用FormData对象上传文件 FormData对象,是可以使用一系列的键值对来模拟一个完整的表单,然后使用XMLHttpRequest发送这个"表单" ...
- java上传图片损坏_iview 文件上传二进制文件提示文件已经损坏
使用iview的 Upload组件结合后端spring mvc做的文件上传,发现文本文件格式上传没问题,但上传其他格式的,比如 doc.xls.jar等格式文件,上传到后端后,打开均会提示文件已经损坏 ...
- 解决python发送multipart/form-data请求上传文件的问题
解决python发送multipart/form-data请求上传文件的问题 参考文章: (1)解决python发送multipart/form-data请求上传文件的问题 (2)https://ww ...
- 使用FormData格式上传图像并预览图片
前言 做项目时,遇到表单中图像需要跟表单一起提交,这样会造成后台没办法接收到图片.后面上网调查后,明白表单提交时是默认application/x-www-form-urlencoded格式,只接受键值 ...
- AWS Boto3 S3对象上传与下载
什么是 Amazon S3? 1.Amazon Simple Storage Service(Amazon S3)是一种对象存储服务,提供行业领先的可扩展性.数据可用性.安全性和性能.各种规模和行业的 ...
- *使用quill富文本编辑器的插件,自定义formData图片上传
官网地址:https://quilljs.com 项目需求:自带的上传图片是base64,直接入库的话,请求头太长,导致网页十分十分的卡,所以才改用formData自定义上传(改完之后,相当于覆盖了他 ...
- RestTemplate发送form-data请求上传rul资源文件及对象参数
需求 上传文件服务中的文件到其他平台 接口描述:用于上传工程日志相关资料 请求url:/cq-szh-projectdocumentscomputesvc/api/service/addEnginee ...
- ajax formdata提交上传,Ajax提交用FormData()上传文件
1.form声明如下 2.ajax设置如下 var formData = new FormData(document.getElementById("form")); $.ajax ...
- java接受formdata文件上传_java后端发送formdata上传文件
今天想实现 java 后端发送 formdata 上传文件,为了以后查找方便,特此记录下来 上一次使用 WebClient 实现远程调用 (一个非阻塞.响应式的HTTP客户端,它以响应式被压流的方式执 ...
- [转载红鱼儿]Delphi实现微信开发(3)如何使用multipart/form-data格式上传文件
开始前,先看下要实现的微信接口,上传多媒体文件,这个接口是用Form表单形式上传的文件.对我来说,对http的Form表单一知半解,还好,查到这个资料,如果你也和我一样,必须看看这篇文章. 在xali ...
最新文章
- C# Math类简介
- android毛玻璃效果,Android 中实现毛玻璃效果
- 红曲面怎么做_「曲面建模」CREO陶瓷小摆件的曲面建模,怎么样分析和拆解面...
- 【转】jQuery最佳实践
- 滑动轮播_这样运营轮播图可以增加订单
- N使用exus2打造企业maven仓库(三)
- 64位Ubuntu kylin 16.04搭建nfs网络文件系统
- int指令01 - 零基础入门学习汇编语言64
- 理科都要学大学计算机吗,女生不适合学理科专业?报考这些理科专业,一毕业就会遭到疯抢!...
- java 全局代码区_MyEclipse设置全局编码
- web文件上传(三)--webapi后台接收参数和文件
- I00020 计算卡特兰数函数
- 为什么要使用自增ID作为主键
- 关于FlyMcu和XCOM软件下载程序的使用(关于STM32F4实现串口通信后续)
- OpenMMLab全景图
- SpringbootKaka批量消费报Listener method could not be invoked with the incoming message
- 蚁群算法求最值c语言实现,蚁群算法代码(求函数最值)
- 有趣又漂亮的可视化图表制作
- FPGA的NIOS-||的开发入门
- Lect1 图像分类