Haorooms
首頁(yè)
文章存檔
關(guān)于我
前端導(dǎo)航
前端資料庫(kù)
小工具
合作
轉(zhuǎn)載自:http://www.haorooms.com/post/input_file_leixing
1.ajax上傳文件
HTML5的 input:file上傳類(lèi)型控制
2014年8月29日
175731次瀏覽
一、input:file屬性
屬性值有以下幾個(gè)比較常用:
accept:表示可以選擇的文件MIME類(lèi)型,多個(gè)MIME類(lèi)型用英文逗號(hào)分開(kāi),常用的MIME類(lèi)型見(jiàn)下表。
multiple:是否可以選擇多個(gè)文件,多個(gè)文件時(shí)其value值為第一個(gè)文件的虛擬路徑。
1、accept
只能選擇png和gif圖片
<input id="fileId1" type="file" accept="image/png,image/gif" name="file" />
2、multiple
多文件上傳
<input id="fileId2" type="file" multiple="multiple" name="file" />
3、常用MIME類(lèi)型
后綴名 MIME名稱(chēng).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
二、樣式美化
請(qǐng)看博客:css input[type=file] 樣式美化,input上傳按鈕美化 http://www.haorooms.com/post/css_input_uploadmh
三、AJAX上傳文件
在說(shuō)到ajax上傳文件。ajax上傳的時(shí)候,需要獲得input:file選擇的文件(可能為多個(gè)文件),獲取其文件列表為:
// input標(biāo)簽的files屬性document.querySelector("#fileId").files// 返回的是一個(gè)文件列表數(shù)組
獲得的文件列表,然后遍歷插入到表單數(shù)據(jù)當(dāng)中。即:
// 獲得上傳文件DOM對(duì)象var oFiles = document.querySelector("#fileId");// 實(shí)例化一個(gè)表單數(shù)據(jù)對(duì)象var formData = new FormData();// 遍歷圖片文件列表,插入到表單數(shù)據(jù)中for (var i = 0, file; file = oFiles[i]; i++) { // 文件名稱(chēng),文件對(duì)象 formData.append(file.name, file);}
獲得表單數(shù)據(jù)之后,就可以用ajax的POST上傳。
// 實(shí)例化一個(gè)AJAX對(duì)象var xhr = new XMLHttpRequest();xhr.onload = function() { alert("上傳成功!");}xhr.open("POST", "upload.php", true);// 發(fā)送表單數(shù)據(jù)xhr.send(formData);
上傳到服務(wù)器之后,獲取到文件列表為:
Array( [jpg_jpg] => Array ( [name] => jpg.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\phpA595.tmp [error] => 0 [size] => 133363 ) [png_png] => Array ( [name] => png.png [type] => image/png [tmp_name] => D:\xampp\tmp\phpA5A6.tmp [error] => 0 [size] => 1214628 ))
在服務(wù)端循環(huán)遍歷這個(gè)數(shù)組就可以上傳文件了。
PREVIOUS:
NEXT:
css input[type=file] 樣式美化,input上傳按鈕美化
jqueryeasyUI dialog 彈出窗口超出瀏覽器,導(dǎo)致不能關(guān)閉的bug解決方案
Tags: html5, input, type, css3, input file
相關(guān)文章:
input file 文件上傳,js控制上傳文件的大小和格式
css input[type=file] 樣式美化,input上傳按鈕美化
HTML5的WebSocket的介紹及應(yīng)用
html5之sse服務(wù)器發(fā)送事件EventSource介紹
canvas繪制圓角環(huán)形圖
HTML5之離線緩存的方法
canvas的ImageData對(duì)象的介紹及應(yīng)用
制作h5婚禮邀請(qǐng)函動(dòng)畫(huà)總結(jié)
iOS下的 Fixed + Input 調(diào)用鍵盤(pán)的時(shí)候fixed無(wú)效問(wèn)題解決方案
css3動(dòng)畫(huà)沿軌跡運(yùn)動(dòng),css3物體沿圓環(huán)運(yùn)動(dòng)
談?wù)刢ss3的混合模式
談?wù)劸W(wǎng)頁(yè)中實(shí)現(xiàn)逐幀動(dòng)畫(huà)
css3的clip-path屬性介紹
css的不常用效果總結(jié)
css對(duì)話框的寫(xiě)法
登錄
**
還沒(méi)有評(píng)論,快來(lái)?yè)屔嘲l(fā)吧!
**關(guān)于haorooms博客
**
haorooms博客,是Aaron個(gè)人技術(shù)博客,主要記錄和總結(jié)前端工作中常用的知識(shí)及我的生活。
訂閱博客文章,請(qǐng)關(guān)注下方微信號(hào),后期會(huì)通過(guò)訂閱號(hào)將文章發(fā)送到您的手機(jī)上。
博客文章轉(zhuǎn)載及廣告合作請(qǐng)點(diǎn)擊
關(guān)鍵詞搜索
最新文章由iPhoneX適配問(wèn)題想到的
css開(kāi)發(fā)技巧二之常見(jiàn)css題目
手腕舟狀骨骨折手術(shù)后第一篇文章
HTML5的WebSocket的介紹及應(yīng)用
html5之sse服務(wù)器發(fā)送事件EventSource介紹
canvas繪制圓角環(huán)形圖
微信視頻播放器隱藏播放控件,視頻全屏做法分享
Es6中Map對(duì)象和Set對(duì)象的介紹及應(yīng)用
HTML5之離線緩存的方法
canvas的ImageData對(duì)象的介紹及應(yīng)用
云標(biāo)簽UEditornavigatorflashjqueryeasyUIsassjsonseophpinput美化iframesublimecanvasIE瀏覽器EcshopjsmartRegExpangularjsantcss hackyuicompressormongodbinput filego語(yǔ)言jquery編輯器手機(jī)網(wǎng)站前端linux心得Emmetjavascriptnginxhtml復(fù)文本框閉包Cookiehtml5dedecmsjscss3nodejsphotoshopexecCommand時(shí)間戳
我的: csdn博客 | 新浪微博 | 知乎 | 昵圖網(wǎng) | github | 網(wǎng)站地圖Copyright ? Aaron All Rights Reserved 滬ICP備14034283號(hào)-1

