亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當前請求不是多部分請求

當前請求不是多部分請求

蠱毒傳說 2022-07-27 21:33:30
我正在嘗試將圖像發送到我的服務器。我不斷收到錯誤消息:當前請求不是多部分請求。當我在 Postman 中測試它時,它工作正常。這是我的html表單:function saveImageToProduct() {    var formData = new FormData(document.querySelector("#newImagesForm"));    var encData = new URLSearchParams(formData.entries());    fetch("/uploadFile", { method: 'POST', body: encData })        .then(response => Promise.all([response.status, response.json()]))        .then(function([status, myJson]) {            if (status == 200) {                console.log("succeed!");            } else {                console.log("failed!");            }        })        .catch(error => console.log(error.message));    return false;}<form enctype="multipart/form-data" novalidate="novalidate" id="newImagesForm" method="post">    <div>        <p>Selecteer een afbeelding:</p>        <input id="file" name="file" type="file"/>    </div>    <br>    <div>        <button id="button" onclick="return saveImageToProduct()" class="btn btn-lg btn-info btn-block">            <span>Voeg aanbieding toe</span>        </button>    </div></form>后端Java代碼:@PostMapping("/uploadFile")public ProductImage uploadFile(@RequestParam("file") MultipartFile file) {    String fileName = fileStorageService.storeFile(file);    String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()            .path("/uploads/")            .path(fileName)            .toUriString();    return new ProductImage(fileName, fileDownloadUri,            file.getContentType(), file.getSize());}當我嘗試發送圖像時,我在后端收到 500 錯誤:2019-03-10 19:40:33.588 ERROR 5668 --- [io-9001-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request] with root cause org.springframework.web.multipart.MultipartException: Current request is not a multipart request當我在 Postman 中執行此操作時,它可以正常工作,如下圖所示:
查看完整描述

2 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

下面的代碼應該可以完成這項工作:您基本上創建了一個新的 Form 對象并將文件數據附加到它。您可以通過添加更多“data.append”行來為其添加多個數據屬性。


    function uploadPicture() {

        var input = document.querySelector('input[type="file"]')

        console.log(productID);

        var data = new FormData()

        data.append('file', input.files[0])

        fetch('/uploadFile/', {

            method: 'POST',

            body: data

        })

        .then(response => Promise.all([response.status, response.json()]))

        .then(function([status, myJson]) {

            if (status == 200) {

                console.log("succeed!");

            } else {

                console.log("failed!");

            }

        })

        .catch(error => console.log(error.message));

    }

HTML:


        <input type="file" name="file" id="fileinput">

        <input type="submit" value="Upload" onclick="uploadPicture()">


查看完整回答
反對 回復 2022-07-27
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您可以嘗試修改它-

var formData = new FormData(document.querySelector("#newImagesForm")[0]);


查看完整回答
反對 回復 2022-07-27
  • 2 回答
  • 0 關注
  • 274 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號