vue 上傳文件方式和上傳文件格式和大小限制 實現

其實打開文件資源對話框的方式有好幾種,但是世界使用input控件是最直接和方便的那種。

ok,直接先上代碼。

<zl-button size="mini" class="upload-file">
	<input @change="fileUpload" type="file" accept=".xls,.xlsx" ref="uploadFile" v-if="!isUping"/>
	<font v-if="!isUping">上傳文件</font>
	<cite v-if="isUping" :style="{width:progressWidth+'%'}" >{{progressWidth+"%"}}</cite>
</zl-button>

其中input是打開文件資源對話框的控件,type='file', accept是默認可接收的文件格式,也就是默認篩選的文件格式,@change事件是你選擇了文件文件,點擊打開按鈕之後,觸發的事件。

至於爲什麼一起封裝在一個button裏面,是爲了能顯示一個類似加載文件的進度條,這個看需求,可加可不加。

現在主要來看如何篩選文件。

其實accept只能篩選默認的文件格式,比如這裏,我只是默認篩選.xls,.xlsx,但是如果你選擇所有文件,還是能上傳別的格式的文件的,如下:

如果你選擇顯示所有格式的文件,那其實這個accept就是個擺設而已。

所有最好還是要在js代碼裏,手動再篩選,限制一次。

先上代碼:

其中,獲取上傳文件的方法有兩種:

1,通過 ref 獲取

2,通過id獲取

然後,獲取上傳的文件的後綴名,方法也有多種,這裏就直接用分割字符串的方式,獲取後綴名,然後與允許的文件格式後綴名數組進行比對。

當然,還有一種方式,可以不需要獲取後綴名,直接拿上傳文件可接收的文件Media Types(MIME)在accept上做限制。

常見文件格式MIME對照表如下:

擴展名 MIME 描述

*.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video
*.ac3 audio/ac3 AC3 Audio
*.asf allpication/vnd.ms-asf Advanced Streaming Format
*.au audio/basic AU Audio
*.css text/css Cascading Style Sheets
*.csv text/csv Comma Separated Values
*.doc application/msword MS Word Document
*.dot application/msword MS Word Template
*.dtd application/xml-dtd Document Type Definition
*.dwg image/vnd.dwg AutoCAD Drawing Database
*.dxf image/vnd.dxf AutoCAD Drawing Interchange Format
*.gif image/gif Graphic Interchange Format
*.htm text/html HyperText Markup Language
*.html text/html HyperText Markup Language
*.jp2 image/jp2 JPEG-2000
*.jpe image/jpeg JPEG
*.jpeg image/jpeg JPEG
*.jpg image/jpeg JPEG
*.js text/javascript, application/javascript JavaScript
*.json application/json JavaScript Object Notation
*.mp2 audio/mpeg, video/mpeg MPEG Audio/Video Stream, Layer II
*.mp3 audio/mpeg MPEG Audio Stream, Layer III
*.mp4 audio/mp4, video/mp4 MPEG-4 Audio/Video
*.mpeg video/mpeg MPEG Video Stream, Layer II
*.mpg video/mpeg MPEG Video Stream, Layer II
*.mpp application/vnd.ms-project MS Project Project
*.ogg application/ogg, audio/ogg Ogg Vorbis
*.pdf application/pdf Portable Document Format
*.png image/png Portable Network Graphics
*.pot application/vnd.ms-powerpoint MS PowerPoint Template
*.pps application/vnd.ms-powerpoint MS PowerPoint Slideshow
*.ppt application/vnd.ms-powerpoint MS PowerPoint Presentation
*.rtf application/rtf, text/rtf Rich Text Format
*.svf image/vnd.svf Simple Vector Format
*.tif image/tiff Tagged Image Format File
*.tiff image/tiff Tagged Image Format File
*.txt text/plain Plain Text
*.wdb application/vnd.ms-works MS Works Database
*.wps application/vnd.ms-works Works Text Document
*.xhtml application/xhtml+xml Extensible HyperText Markup Language
*.xlc application/vnd.ms-excel MS Excel Chart
*.xlm application/vnd.ms-excel MS Excel Macro
*.xls application/vnd.ms-excel MS Excel Spreadsheet
*.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet MS Excel Spreadsheetml.sheet
*.xlt application/vnd.ms-excel MS Excel Template
*.xlw application/vnd.ms-excel MS Excel Workspace
*.xml text/xml, application/xml Extensible Markup Language
*.zip aplication/zip Compressed Archive

更詳細的看這個鏈接:http://www.iana.org/assignments/media-types/media-types.xhtml

關於文件大小限制,就相對來說更簡單了。

追後根據後端接口,把文件傳給後端就ok了。

    let params = new FormData()
    params.append('file', m.file)
	params.append('name', m.file.name)
	ajax.call(m,url,params,(res,err)=>{
		if(res.status!="200"){
			m.$message.warning(res.data.msg)
		}else{
			m.$message.success("上傳成功")
			m.$emit('importDeviceSuccess')
		}
		setTimeout(()=>{
			m.isUping=false
		},2000)
	},m.isRequest, undefined, true)

 

這是一個比較簡單,正常的上傳文件流程,具體的特殊情況可能還需要具體再討論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章