首先我們來了解一下上傳文件
<input type="file">
input的file常用上傳類型
后綴名 MIME名稱
*.3gpp audio/3gpp, video/3gpp
*.ac3 audio/ac3
*.asf allpication/vnd.ms-asf
*.au audio/basic
*.css text/css
*.csv text/csv
*.doc application/msword
*.dot application/msword
*.dtd application/xml-dtd
*.dwg image/vnd.dwg
*.dxf image/vnd.dxf
*.gif image/gif
*.htm text/html
*.html text/html
*.jp2 image/jp2
*.jpe image/jpeg
*.jpeg image/jpeg
*.jpg image/jpeg
*.js text/javascript, application/javascript
*.json application/json
*.mp2 audio/mpeg, video/mpeg
*.mp3 audio/mpeg
*.mp4 audio/mp4, video/mp4
*.mpeg video/mpeg
*.mpg video/mpeg
*.mpp application/vnd.ms-project
*.ogg application/ogg, audio/ogg
*.pdf application/pdf
*.png image/png
*.pot application/vnd.ms-powerpoint
*.pps application/vnd.ms-powerpoint
*.ppt application/vnd.ms-powerpoint
*.rtf application/rtf, text/rtf
*.svf image/vnd.svf
*.tif image/tiff
*.tiff image/tiff
*.txt text/plain
*.wdb application/vnd.ms-works
*.wps application/vnd.ms-works
*.xhtml application/xhtml+xml
*.xlc application/vnd.ms-excel
*.xlm application/vnd.ms-excel
*.xls application/vnd.ms-excel
*.xlt application/vnd.ms-excel
*.xlw application/vnd.ms-excel
*.xml text/xml, application/xml
*.zip aplication/zip
*.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
accept屬性
只能選擇png和gif圖片
<input id="fileId1" type="file" accept="image/png,image/gif" name="file" />
multiple屬性
多文件上傳
<input id="fileId2" type="file" multiple="multiple" name="file" />
獲取file的內(nèi)容
這里要用到change()函數(shù),當元素的值發(fā)生改變時,會發(fā)生 change 事件。
$('input[type="file"]').change(function() {
file = this.files[0];
});
file獲取出來的內(nèi)容
File {name: "DSC03891.JPG", lastModified: 1451217306000, lastModifiedDate: Sun Dec 27 2015 19:55:06 GMT+0800 (中國標準時間), webkitRelativePath: "", size: 3866624…}lastModified: 1451217306000lastModifiedDate: Sun Dec 27 2015 19:55:06 GMT+0800 (中國標準時間)name: "DSC03891.JPG"size: 3866624type: "image/jpeg"webkitRelativePath: ""__proto__: File
我們需要將圖片文件轉(zhuǎn)換成二進制流的方式傳給后臺,這里我介紹兩種方式
FileReader
這一篇博客對FileReader的使用介紹得很詳盡,請參看
http://blog.csdn.net/zk437092645/article/details/8745647/
FormData
FormData有兩種用法
- 提交表單數(shù)據(jù)
提交表單還有一個方法是serialize(),但這個方法不能上傳文件
使用方法
- 在html的form標簽里添加 enctype ="multipart/form-data",這個很重要,沒有這個后面所有都沒效果
<form class="form-horizontal" id="productform" enctype ="multipart/form-data">
- 在html表單的input中添加name屬性,name對應數(shù)據(jù)庫中的字段名
<input type="text" name="productName">
<input type="text" name="productContent">
- ajax交互
$('#submit').click(function(){
var data = new FormData($('#productform')[0]);//[0]必須要
$.ajax({
type:"POST",
url:"http://localhost/blog/management/insertProduct",
data:data,
async:false,
cache:false,
contentType:false,
processData:false,
success:function() {
alert("保存成功!");
},
error:function() {
alert("保存失敗!");
console.log(productType);
}
})
});
- 直接使用FormData
FormData也可以直接使用而不是提交整個表單
<input type="file" name="picture" accept=".jpg,.png"
<input type="button" name="" value="提交" class="submit">
$('.submit').click(function() {
var bannerIdText = $(this).closest('div').find(".bannerId").text();
console.log($(".bannerId").html());
var data = new FormData();
data.append("picture",file);
console.log(bannerIdText);
data.append("bannerId",bannerIdText);
console.log(data);
$.ajax({
type:"POST",
url:" http://localhost/blog/management/updateBanner",
data:data,
async:false,
cache:false,
contentType:false,
processData:false,
enctype: 'multipart/form-data',//沒有了在form聲明處理方式,就必須在這里聲明
success:function(data) {
alert("保存成功!");
console.log(data);
},
error:function() {
alert("保存失敗!");
}
})
});
FormData的append參數(shù)說明
data.append(數(shù)據(jù)庫字段名,你添加的東西名字)
最后如果前端成功的將數(shù)據(jù)發(fā)給后臺了,你可以在network中看到這樣的信息。
image.png