Vue
<el-upload
ref="upload"
:auto-upload='false'
:multiple='false'
:before-upload="beforeUpload"
:limit="1"
accept=".txt"
action=""
type="file"
:http-request="chooseFile">
<el-button class="reportFilebtn"> 选择文件</el-button>
</el-upload>
method
beforeUpload(file) {
const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
const whiteList = ["txt"];
if (whiteList.indexOf(fileSuffix) === -1) {
this.$message.error('上传文件只能是txt格式');
return false;
}
},
chooseFile(file) {
this.$refs.upload.clearFiles()
this.file = file.file
var forms = new FormData()
var configs = {
headers:{'Content-Type':'multipart/form-data'}
}
forms.append('errList',this.uartlogForm.errList)
forms.append('uartFile',this.file)
this.logCheckStation = []
uartLogTool(forms, configs).then(response => {
})
},
Api.js
export const uartLogTool = (params)=>{
return axios.post(`tool/uartParseErr`,params)
}
Py文件
def uartParseErr(request):
if request.method == 'POST':
response = {}
uartFileData = request.FILES.get('uartFile', None)
dutErrStr = request.POST.get('dutErrList','ERROR,Fail,Not Found').strip(',,')
smokeyErrStr = request.POST.get('smokeyErrList','Not Found,All errors').strip(',,')
storageTempPath = default_storage.save('tmp/{}'.format(uartFileData), ContentFile(uartFileData.read()))
uartFilePath = os.path.join(settings.MEDIA_ROOT,storageTempPath)
with open(uartFilePath,'r',encoding="utf-8") as file:
log_data = file.read()
try:
response['uartData'] = dict_uart_log_data
response['stationID'] = STATION_ID
response['overlayVer'] = OVERLAY_VERSION
response['msg'] = 'success'
response['error_num'] = 0
except Exception as e:
response['msg'] = str(e)
response['error_num'] = 1
os.remove(uartFilePath)
return JsonResponse(response)