$.ajax({
url: '',
type: 'POST',
cache: false,
data: new FormData($('#formId')[0]),
processData: false,
contentType: false)}
这里要注意几点:
processData
设置为false
。因为data
值是FormData
对象,不需要对数据做处理。<form>
标签添加enctype="multipart/form-data"
属性。cache
设置为false
,上传文件不需要缓存。contentType
设置为false,不设置contentType值,
因为是由<form>
表单构造的FormData
对象,且已经声明了属性enctype="multipart/form-data"
,所以这里设置为false
FormData对象兼容性如下:
Desktop
Feature | Chrome | Firfox(Gecko) | Intenet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 7+ | 4.0(2.0) | 10+ | 12+ | 5+ |
append with filename | (Yes) | 22.0(22.0) | ? | ? | ? |
delete, get, getAll, has, set | Behind Flag | Not supported | Not supported | (Yes) | Not supported |
Mobile
Feature | Android | Chrome Android | Firfox Mobile (Gecko) | Firfox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 3.0 | ? | 4.0(2.0) | 1.0.1 | ? | 12+ | ? |
append with filename | ? | ? | 22.0(22.0) | 1.2 | ? | ? | ? |
delete, get, getAll, has, set | (Yes) | (Yes) | Not supported | Not supported | Not supported | (Yes) | Not supported |
引用: