Javascript讀取上傳文件內(nèi)容/類型/字節(jié)數(shù)
在網(wǎng)站開發(fā)的某些情況下我們需要上傳文件到服務(wù)器,在這個(gè)過程中可能會對文件做一定的限制,比如說文件格式,文件大小等,在一些情況下我們上傳文件其實(shí)是為了獲取其中的內(nèi)容在前端區(qū)域展示,這個(gè)時(shí)候就不需要將文件上傳到服務(wù)器,完全可以通過Javascript來獲取上傳文件內(nèi)容然后進(jìn)行展示,既加快了操作速度,也減輕了服務(wù)器的負(fù)載和存儲。接下來就是一個(gè)實(shí)際操作的過程:
首先來看一下一個(gè)上傳文件對象的屬性:

UI設(shè)計(jì)(React+Material-ui)
...
const styles = theme => ({
formControl: {
margin: theme.spacing.unit,
minWidth: 120,
width: '100%',
},
leftIcon: {
marginRight: theme.spacing.unit,
}
})
...
<Grid item xs>
<FormControl
className={classes.formControl}
error={this.state.Err.includes('sqlStr')}
>
<TextField
label="SQL"
onChange={this.onTextChange('sqlStr')}
value={this.state.sqlStr}
placeholder="Add Select SQL here..."
multiline
InputLabelProps={{
shrink: true,
}}
fullWidth
rows={6}
variant="outlined"
/>
<FormHelperText>{this.state.sqlStrErr}</FormHelperText>
<input
style={{display: 'none'}}
name="uploadSqlFile"
id="uploadSqlFile"
onChange={this.handleUploadSqlFile}
type="file"
/>
<label htmlFor="uploadSqlFile" style={{position: 'absolute', right: '0px',bottom: '20px', background:'#f5f0ff'}}>
<Button color="primary" variant="outlined" component="span">
<CloudUploadOutlined className={classes.leftIcon} />OR UPLOAD SQL FILE
</Button>
</label>
</FormControl>
</Grid>
...
效果圖如下:

操作綁定,分別包含前端文件內(nèi)容讀取和文件上傳
handleUploadSqlFile = event => {
let that = this
const selectedFile = event.target.files[0]
if(selectedFile.type.includes('text') || selectedFile.type === ''){
let reader = new FileReader();// !important
reader.readAsText(selectedFile, "UTF-8");// !important
reader.onload = function(evt){// !important
let sqlStr = evt.target.result;// !important
that.setState({
Err: that.state.Err.filter(c => c !== 'sqlStr'),
sqlStr: sqlStr,
sqlStrErr: '*Avoid duplicated column fields',
})
}
}else {
let sqlStrErr = 'File format is not supported!'
if ((selectedFile.size / 1024 / 1024).toFixed(4) >= 2) {//計(jì)算文件大小并且換算成M為單位
sqlStrErr = 'File size exceeds the limitation (2M)!'
}
this.setState({
Err: [...this.state.Err, 'sqlStr'],
sqlStrErr: sqlStrErr
})
}
}
上邊的示例只是單純的前端文件內(nèi)容讀取,并未涉及文件上傳到服務(wù)器,接下來是:
import axios from 'axios'
...
handleUploadSqlFile = event => {
const selectedFile = event.target.files[0]
if ((selectedFile.size / 1024 / 1024).toFixed(4) >= 10) {
this.setState({ sqlStrErr: 'File size exceeds the limitation (10M)!' })
} else {
const data = new FormData()
data.append('file', selectedFile, selectedFile.name)
axios
.post('/api/utils/upload_file', data, {
onUploadProgress: ProgressEvent => {
this.setState({
loaded: (ProgressEvent.loaded / ProgressEvent.total) * 100 - Math.random() * 16,//此值用來展示上傳進(jìn)度,好讓用戶知道目前的上傳狀態(tài)。
})
},
})
.then(res => {
if (res.data.code === -1) {
this.setState({ sqlStrErr: res.data.info })
} else {
this.setState({
loaded: 100,
})
}
})
}
}
如果看了上邊的代碼示例還搞不定歡迎留言提問!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用webpack搭建pixi.js開發(fā)環(huán)境
這篇文章主要介紹了使用webpack搭建pixi.js開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
淺析Javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制
這篇文章給大家詳細(xì)介紹了javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下吧2017-10-10
JS實(shí)現(xiàn)禁止鼠標(biāo)右鍵的功能
遇到網(wǎng)頁上有精美圖片或者精彩文字想保存時(shí),通常大家都是選中目標(biāo)后按鼠標(biāo)右鍵,在彈出菜單中選擇“圖片另存為”或“復(fù)制”來達(dá)到我們的目的。但是,目前有許多網(wǎng)頁都屏蔽了鼠標(biāo)右鍵,那么用js如何實(shí)現(xiàn)禁止鼠標(biāo)右鍵的功能呢?下面小編給大家介紹下2016-10-10
JavaScript庫之vanilla-tilt使用教程(一個(gè)平滑的3D傾斜庫)
vanilla-tilt.js是Javascript中一個(gè)平滑的3D傾斜庫,可以讓網(wǎng)頁的一些控件變得動態(tài)起來,下面這篇文章主要給大家介紹了關(guān)于JavaScript庫之vanilla-tilt使用的相關(guān)資料,需要的朋友可以參考下2023-02-02
原生js實(shí)現(xiàn)shift/ctrl/alt按鍵的獲取
小測試shift、ctrl、alt按鍵的獲取,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04
在SSM框架下用laypage和ajax實(shí)現(xiàn)分頁和數(shù)據(jù)交互的方法
今天小編大家分享一篇在SSM框架下用laypage和ajax實(shí)現(xiàn)分頁和數(shù)據(jù)交互的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

