[英]Payload too large error (413) when uploading using JS Fetch API. Works fine via Postman
我有一個分段上傳 API,當我通過 Postman 上傳文件時,它可以完美運行。 但是,當我通過相同的 API 上傳但使用 Fetch API 時,出現 413 錯誤。
我的JS代碼如下 -
async function uploadAudio(formElements) {
const formData = new FormData();
const file = fileUploadField.files[0];
formData.append("file", file);
console.log("Name: " + file.name); // Prints the correct file name
console.log("Size: " + file.size); // Prints the correct size (which is < than what is set server side)
const options = {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': 'Bearer ' + authTokenArr[0]
}
};
const response = await fetch(base_url + "/audio/file", options);
const data = await response.json();
if (response.ok) {
handleSuccess(data);
} else {
handleFail(data);
}
}
application.properties
文件 -
# max file size
spring.servlet.multipart.max-file-size=10MB
# max request size
spring.servlet.multipart.max-request-size=10MB
REST 控制器 -
@PostMapping("file", produces = [ProduceTypes.JSON])
fun uploadAudio(@RequestParam("file") file: MultipartFile,
@RequestHeader (name="Authorization") token: String): ResponseEntity<MutableMap<String, Any>> {
val user: User = userService.getUserDetailsByToken(token)
val body: MutableMap<String, Any> = LinkedHashMap()
body["status"] = ApiStatus.UPLOADED
body["fileId"] = podcastFileService.uploadFileToB2(user, file)
return ResponseEntity(body, HttpStatus.CREATED)
}
這是我的服務器端日志 -
2020-09-03 18:56:02.288 DEBUG 40716 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : POST "/audio/file", parameters={}
2020-09-03 18:56:02.293 DEBUG 40716 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.krtkush.audiotime.globalexceptions.GlobalExceptionHandler#handleMultipartException(MultipartException)
2020-09-03 18:56:02.356 DEBUG 40716 --- [nio-8080-exec-7] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
2020-09-03 18:56:02.357 DEBUG 40716 --- [nio-8080-exec-7] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=2020-09-03T18:56:02.297867, message=Maximum file size acceptable is 10MB}]
2020-09-03 18:56:02.364 DEBUG 40716 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found]
2020-09-03 18:56:02.364 DEBUG 40716 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Completed 413 PAYLOAD_TOO_LARGE
當 API 通過 Postman 正常工作時,甚至使用帶有非 REST 控制器的mustache
模板上傳時,為什么會出現大小問題。
結果是'Content-Type': 'multipart/form-data'
是問題所在。 當我刪除它時它起作用了。 @Kostiantyn
評論在這里幫助了我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.