STATIC_URL = "statics/"
STATICFILES_DIRS = [
os. path. join( BASE_DIR, "../statics" ) ,
]
前端创建项目:
前端增加了暂停和继续上传功能具体代码:
< template>
< div class = " button" >
< el-upload
ref = " uploadRef"
class = " upload-demo"
:http-request = " uploadFile"
:show-file-list = " false"
>
< el-button type = " primary" > 点击上传文件</ el-button>
</ el-upload>
</ div>
< div>
< el-button @click = " is_paused" type = " primary" > 暂停</ el-button>
< el-button @click = " is_continue" type = " success" > 继续</ el-button>
</ div>
</ template>
< script setup >
import axios from 'axios'
import { sha256} from 'js-sha256'
import { ref} from "vue" ;
const flag = ref ( true )
const axios_list = ref ( [ ] )
const sha256Promise = ref ( '' )
const checkUploadedChunks = ( sha256Promise ) => {
return axios. post ( 'http://127.0.0.1:8000/api/check' , {
sha256Promise : sha256Promise
} ) . then ( response => {
return response. data;
} ) ;
} ;
const uploadFile = ( { file} ) => {
axios_list. value = [ ]
flag. value = true
const chunkSize = 4 * 1024 * 1024 ;
const fileSize = file. size;
const chunks = Math. ceil ( fileSize / chunkSize) ;
sha256Promise. value = sha256 ( file. name) ;
return checkUploadedChunks ( sha256Promise. value) . then ( async uploadedChunks => {
if ( uploadedChunks. length === chunks) {
console. log ( "已经上传完成就不需要再重复上传" )
return Promise. resolve ( ) ;
}
for ( let i = 0 ; i < chunks; i++ ) {
const formData = new FormData ( ) ;
if ( uploadedChunks. includes ( i + 1 ) ) {
console. log ( "跳过已上传部分片段:" , i + 1 ) ;
continue ;
}
const start = i * chunkSize;
const chunk = file. slice ( start, start + chunkSize) ;
formData. append ( 'chunk' , chunk) ;
formData. append ( 'chunkNumber' , i + 1 ) ;
formData. append ( 'chunksTotal' , chunks) ;
formData. append ( 'sha256Promise' , sha256Promise. value) ;
formData. append ( 'filename' , file. name) ;
if ( flag. value) {
const res = await axios. post ( 'http://127.0.0.1:8000/api/upload' , formData)
} else {
axios_list. value. push ( formData)
}
}
} ) ;
} ;
const is_paused = ( ) => {
flag. value = false
}
const is_continue = async ( ) => {
if ( axios_list. value. length> 0 ) {
checkUploadedChunks ( axios_list. value[ 0 ] . get ( "sha256Promise" ) ) . then ( async uploadedChunks => {
for ( const item of axios_list. value) {
let chunkNumber = parseInt ( item. get ( "chunkNumber" ) )
if ( ! uploadedChunks. includes ( chunkNumber) ) {
await axios. post ( 'http://127.0.0.1:8000/api/upload' , item)
if ( ! flag. value) {
break
}
} else {
console. log ( "跳过已经上传过的片段号:" , chunkNumber)
}
}
} )
} else {
}
flag. value = true
}
</ script>
< style >
html, body {
height : 100%;
width : 100%;
//background-color : pink;
}
</ style>
效果如下:
点击暂停的效果:
再点击继续