前端vue代码:
data() {
fileList: [],
logo: ‘’,
}
methods: {
onBeforeUploadImage(file) {
const isIMAGE = file.type === ‘image/jpeg’ || ‘image/jpg’ || ‘image/png’
const isLt1M = file.size / 1024 / 1024 < 1
if (!isIMAGE) {
this.KaTeX parse error: Expected 'EOF', got '}' at position 37: …是图片格式!') }̲ if (!isL…message.error(‘上传文件大小不能超过 1MB!’)
}
return isIMAGE && isLt1M
},
UploadImage(param){
const formData = new FormData()
formData.append(‘file’, param.file)
UploadImageApi(formData).then(response => {
console.log(‘上传图片成功’)
param.onSuccess() // 上传成功的图片会显示绿色的对勾
// 但是我们上传成功了图片, fileList 里面的值却没有改变,还好有on-change指令可以使用
}).catch(response => {
console.log(‘图片上传失败’)
param.onError()
})
},
fileChange(file){
this.$refs.upload.clearFiles() //清除文件对象
this.logo = file.raw // 取出上传文件的对象,在其它地方也可以使用
this.fileList = [{name: file.name, url: file.url}] // 重新手动赋值filstList, 免得自定义上传成功了, 而fileList并没有动态改变, 这样每次都是上传一个对象
},
}

后端python代码:
def upload_file(request):
if request.method == “POST”: # 请求方法为POST时,进行处理
myFile =request.FILES.get(“myfile”, None) # 获取上传的文件,如果没有文件,则默认为None
if not myFile:
returnHttpResponse(“no files for upload!”)
destination = open(os.path.join(“E:\upload”,myFile.name),‘wb+’) # 打开特定的文件进行二进制的写操作
for chunk in myFile.chunks(): # 分块写入文件
destination.write(chunk)
destination.close()
returnHttpResponse(“upload over!”)

Logo

前往低代码交流专区

更多推荐