以前,我的代码在上传文件时有效。但是,在API和Ajax中添加了“ size”参数后,上传过程似乎出现了错误。但是当我尝试使用直接从API后端上传时成功了。因此,我确定需要确定Ajax中的“大小”,但不知道在哪里。
控制台中的响应错误
{"detail":[{"loc":["body","file"],"msg":"field required","type":"value_error.missing"}]}
HTML
<table id="MyTable" class="table">
<thead>
<tr>
<th class="text-center">Filename</th>
<th class="text-center">Timestamp</th>
<th class="text-center">Uploader</th>
<th class="text-center">Size</th>
<th class="text-center">Action</th>
</tr>
</thead>
</table>
<form name="esvalueForm" id="esvalueForm" action="" method="POST" enctype="multipart/form-data">
<div class="row mb-30">
<div class="col-lg-12 col-sm-12">
<div class="custom-file">
<input type="file" name="upload_file" id="abc" class="import-file">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<button type="button" id="btnUpload" class="btn btn-primary">UPLOAD</button>
</div>
</div>
</form>
JS
$("#btnUpload").on("click",function(){
var MyTable = $("#MyTable").val();
var fd = new FormData();
var files = $('#MyTable')[0].files[0];
fd.append('upload_file',files);
$.ajax({
url: 'URL',
type: 'POST',
data: fd,
xhrFields: {
withCredentials: true
},
contentType: false,
processData: false,
success: function(response){
alert("OK")
},
error: function(){
alert("NOT OK")
},
});
});