我正在尝试将 csv
文件发送到服务器。我使用 FileReaderAPI
加载文件,然后通过 ajax
发送它。但我收到以下错误。
For request 'POST /api/upload' [Missing boundary header]
JS
$('#upload-file-btn').on('click', function(e){
e.preventDefault();
var file = document.getElementById('input_file').files[0];
console.log(file);
reader = new FileReader();
reader.onload = function () {
var source = reader.result;
var payload = {source: source};
console.log(source);
$.ajax({
url:"http://localhost:9000/api/upload",
type:"POST",
data: JSON.stringify(payload),
success: function(data){
console.log(data);
}
});
}
reader.readAsText(file);
});
一些解决方案建议手动包含 "Content-Type": "multipart/form-data"
header 会导致问题。我没有使用但仍然遇到上述问题。
最佳答案
如果您需要将数据作为文件发送,则必须使用 FormData 对象通过 ajax 发送 multipart/form-data 数据。
var fd = new FormData();
fd.append('source', $('#input_file')[0].files[0]);
$.ajax({
url:"http://localhost:9000/api/upload",
type:"POST",
data: fd,
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
关于javascript - Post 请求缺少边界 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52608016/