Skip to content

Commit

Permalink
前端压缩完成
Browse files Browse the repository at this point in the history
  • Loading branch information
ShellBin committed Nov 22, 2020
1 parent 7f826e2 commit 353d1dc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 24 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"axios": "^0.21.0",
"babel-loader": "8.x.x",
"body-parser": "^1.19.0",
"compressorjs": "^1.0.6",
"cors": "^2.8.5",
"css-loader": "^3.2.0",
"express": "4.16.x",
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions server/adminPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
login: require('./login'),
setTask: require('./set-task'),
downloadFiles: require('./download-files')
}
Empty file added server/adminPage/login.js
Empty file.
Empty file added server/adminPage/set-task.js
Empty file.
2 changes: 1 addition & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
host: ''
},
fs: {
path: './uploads-temp/'
path: './uploads/'
},
admin: {
password: 'password',
Expand Down
5 changes: 4 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const cors = require('cors')

const config = require('./config')
const indexPage = require('./indexPage')
const adminPage = require('./adminPage')

const server = express()
const router = express.Router()
const upload = multer({dest: config.fs.path})
const upload = multer({dest: './uploads-temp/'})

server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: false }))
Expand All @@ -21,5 +22,7 @@ server.use('/api/v1/',router)
router.get('/fetchTask',indexPage.fetchTask)
// API: 处理上传的文件
router.post('/uploadFile',upload.single('file'),indexPage.uploadFile)
// API: 处理登录操作
// router.post('/login',adminPage.login)

console.log('Server is running at '+config.http.port+' port, hostname/api/v1/')
1 change: 1 addition & 0 deletions server/indexPage/upload-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function indexStuInfo(stuId) {
}

function uploadFile (req, res) {
console.log(req.file)
if (indexStuInfo(req.headers.stuid)) {
console.log(`${stuName} (${req.connection.remoteAddress}) uploaded ${req.file.originalname}`)

Expand Down
65 changes: 43 additions & 22 deletions src/pages/IndexPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
</template>

<script>
import Compressor from 'compressorjs';
export default {
name: 'IndexPage',
data () {
Expand All @@ -56,39 +58,58 @@ export default {
},
methods: {
uploadStart () {
const thisFile = this.$refs.file.files[0]
if (this.haveStarted === true) {
alert('上传已经开始')
} else {
this.haveStarted = true
}if (parseInt(this.stuId) > 0) {
}if (parseInt(this.stuId) > 0 && thisFile) {
this.displayStatus = '文件正在上传...'
this.displayMessage = '当前进度 0%'
const file = this.$refs.file.files[0]
const data = new FormData()
data.append('file', file)
this.axios.post('http://localhost:8089/api/v1/uploadFile', data, {
headers: {
'Content-Type': 'multipart/form-data',
'stuId': this.stuId
},onUploadProgress: function (progressEvent){
this.displayMessage = '当前进度 ' + (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
} })
.then(res => {
if(res.data.status === 'success') {this.displayStatus = '上传成功'; this.displayMessage = '当前命名:' + res.data.finalFileName}
})
.catch((error) => {
this.displayStatus = '上传失败'
this.displayMessage = '检查学号后三位是否填写正确'
this.isError = true
console.log(error)
})
if(thisFile.type === ('image/png'||'image/jpeg')) {
//压缩并发送
this.compressedPic(thisFile)
} else {
//直接发送
this.uploadFile(thisFile)
}
} else {
this.displayStatus = '还不能开始上传'
this.displayMessage = '你真的有在好好填学号后三位吗'
this.displayMessage = '学号和文件是不是有什么没填好的'
this.isError = true
}
},
compressedPic(pic) {
this.displayMessage = '图片正在压缩'
new Compressor(pic,{
quality:0.5,
maxWidth:720,
success: (result) => {
this.uploadFile(new File([result], result.name, {type: result.type}))
console.log('图片已被压缩')
}
})
},
uploadFile(file) {
const data = new FormData()
data.append('file', file)
this.axios.post('http://localhost:8089/api/v1/uploadFile', data, {
headers: {
'Content-Type': 'multipart/form-data',
'stuId': this.stuId
},onUploadProgress: (progressEvent) => {
this.displayMessage = '当前进度 ' + (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
} })
.then(res => {
if(res.data.status === 'success') {this.displayStatus = '上传成功'; this.displayMessage = '当前命名:' + res.data.finalFileName}
})
.catch((error) => {
this.displayStatus = '上传失败'
this.displayMessage = '检查学号是否正确,网络是否正常'
this.isError = true
console.log(error)
})
},
fetchTask () {
this.axios.get('http://localhost:8089/api/v1/fetchTask')
.then((response) => {
Expand Down

0 comments on commit 353d1dc

Please sign in to comment.