FormData 对象上传二进制文件
XMLHttpRequest发送请求的键/值对。它可以更灵活方便的发送表单数据,因为可以独立于表单使用。如果你把表单的编码类型设置为multipart/form-data ,则通过FormData传输的数据格式和表单通过submit() 方法传输的数据格式相同,也就是二进制文件。
{
lastModified:1247549551674
lastModifiedDate:Tue Jul 14 2009 13:32:31 GMT+0800 (中国标准时间) {}
name:"ju.jpg"
size:879394
type:"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:;
z-index:;
}
.anviz-upload-file .image-item input[type="file"]{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity:;
cursor: pointer;
z-index:;
}
.img-list{
width: 100%;
height: 105px;
padding: 10px 10px;
overflow: hidden;
border-top: 1px solid #c8c7cc;
border-bottom: 1px solid #c8c7cc;
background: #fff;
margin:;
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" onclick="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>
亲测可用!欢迎指正交流。
FormData 对象上传二进制文件的更多相关文章
- 通过jQuery Ajax使用FormData对象上传文件 (转载)
XMLHttpRequest Level 2 添加了一个新的接口——FormData.与普通的 Ajax 相比,使用 FormData 的最大优点就是我们可以异步上传二进制文件.jQuery 2.0+ ...
- 通过jQuery Ajax使用FormData对象上传文件
FormData对象,是可以使用一系列的键值对来模拟一个完整的表单,然后使用XMLHttpRequest发送这个"表单". 在 Mozilla Developer 网站 使用For ...
- IT轮子系列(四)——使用Jquery+formdata对象 上传 文件
前言 在MVC 中文件的上传,一般都采用控件: <h2>IT轮子四——文件上传</h2> <div> <input type="file" ...
- [转] 通过jQuery Ajax使用FormData对象上传文件
FormData对象,是可以使用一系列的键值对来模拟一个完整的表单,然后使用XMLHttpRequest发送这个"表单". 在 Mozilla Developer 网站 使用For ...
- jquery 通过ajax FormData 对象上传附件
之前上传附件都是用插件,或者用form表单体检(这个是很久以前的方式了),今天突发奇想,自己来实现附件上传,具体实现如下 html: <div> 流程图: <input id=& ...
- 利用formdata对象上传文件时,需要添加的参数
function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...
- 基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现
前言 图片上传是web项目常见的需求,我基于之前的博客的代码(请戳:formData批量上传的多种实现)里的第三种方法实现多图片的预览.上传,并且支持三种方式添加图片到上传列表:选择图片.复制粘贴图片 ...
- axios+FormData文件上传
axios+FormData文件上传 原理:FormData上传 创建一个FormData对象,将得到的文件流对象放在FormData内,然后使用axios上传 注意: 1.请求头设置 headers ...
- C# HTTP系列13 以form-data方式上传多个文件以及键值对集合到远程服务器
系列目录 [已更新最新开发文章,点击查看详细] 类似于以下场景,将表单中的用户信息(包含附件)上传到服务器并保存到数据库中, <form id="form1" run ...
随机推荐
- [Swift]LeetCode10. 正则表达式匹配 | Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [Swift]LeetCode518. 零钱兑换 II | Coin Change 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- Python内置函数(39)——locals
英文文档: locals() Update and return a dictionary representing the current local symbol table. Free var ...
- JVM基础系列开篇:为什么要学虚拟机?
跟许多人一样,我一开始接触 Java 虚拟机只是因为面试需要用到,所以硬着头皮看看.所以很多人对于为什么要学虚拟机这个问题,他们的答案都是:因为面试.但我经过了几年的学习和实战,我发现其实学习虚拟机并 ...
- 基于 dubbo 的分布式架构
前言 现在越来越多的互联网公司还是将自己公司的项目进行服务化,这确实是今后项目开发的一个趋势,就这个点再凭借之前的 SSM 项目来让第一次接触的同学能快速上手. 浅谈分布式架构 分布式架构单看这个名字 ...
- Android软键盘事件imeOptions响应
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 在android发开过程中,有时候需要对EditText的软键盘进行监听. 当点击软键盘回车位置按键的时候,需要实现 完成.前进.下 ...
- 从0打卡leetcode之day 3 -- 最大子序列和
前言 就有要把leetcode的题刷完,每天一道题,每天进步一点点 从零打卡leetcode之day 3 题目描述: 给定一个int类型的数组,求最大子序列的和. 也就是说,从这个数组中截取一个子数组 ...
- Babel presets stage
在一些新框架的代码中,常基于es6/7标准来书写代码.鉴于这些标准被没有被浏览器广泛支持,我们一般使用babel来将使用e6/7标准书写的代码降级编译(或者说转译)为浏览器可解析的es3/5代码. 以 ...
- ASP.NET Core框架揭秘(持续更新中…)
之前写了一系列关于.NET Core/ASP.NET Core的文章,但是大都是针对RC版本.到了正式的RTM,很多地方都发生了改变,所以我会将之前发布的文章针对正式版本的.NET Core 1.0进 ...