easypoi导入excel Current request is not a multipart request
时间: 2023-09-10 14:10:22 浏览: 172
If you're getting the "Current request is not a multipart request" error when using EasyPoi to import Excel files, it means that the request being sent to the server is not a multipart/form-data request, which is required for file uploads.
To fix this issue, you need to ensure that your front-end code is correctly sending the file as a multipart/form-data request. Here's an updated version of the front-end code that shows how to properly send a multipart request:
```javascript
const fileInput = document.getElementById('excelFile');
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
const formData = new FormData();
formData.append('file', file);
fetch('/upload', {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data' // Set the Content-Type header to multipart/form-data
}
})
.then(response => response.json())
.then(data => {
// Handle the response from the back-end
console.log(data);
})
.catch(error => {
// Handle any errors
console.error(error);
});
});
```
Make sure to set the `Content-Type` header to `multipart/form-data` when making the POST request.
On the back-end, the code should remain the same as mentioned in the previous response. The `@RequestParam` annotation in the controller method will automatically bind the uploaded Excel file to the `MultipartFile` parameter.
By updating your front-end code to send a proper multipart request, you should be able to resolve the "Current request is not a multipart request" error. Let me know if you have any further questions!
阅读全文