简体   繁体   English

尽管我写了“enctype="multipart/form-data”,但发生错误“当前请求不是多部分请求”

[英]Although I wrote 'enctype="multipart/form-data', ERROR 'Current request is not a multipart request' happen

I am implementing the file upload function.我正在实现文件上传功能。

When I upload a file, it shows what I uploaded, puts in the contents, and posts.当我上传文件时,它会显示我上传的内容,放入内容并发布。

By the way, there was a problem below while uploading the files.顺便说一下,上传文件时出现了下面的问题。

While trying to find a solution, many people asked me to add enctype="multipart/form-data but I added that one earlier.在试图找到解决方案时,很多人要求我添加enctype="multipart/form-data但我之前添加了那个。

And yet I don't understand that kind of error.然而我不明白那种错误。

error错误

2021-07-30 23:08:21.239 ERROR 20744 --- [nio-8080-exec-9] 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

front正面

<template>
  <div class="scale">
    <button><b-icon-arrow-left></b-icon-arrow-left></button><span>게시글 생성하기</span>
    <form enctype="multipart/form-data" method="post" >
      <div class="d-flex flex-row">
        <button style="display:inline-block; margin-right:5%; margin-left:2%" @click.prevent="clickInputTag()" id='addimage'><b-icon-plus class="h1"></b-icon-plus></button>
        <input hidden ref="plus" name="file" id="file" type="file"  accept="image/*" @change.prevent="uploadImage($event)" multiple>
        <div id="image_container"></div>
      </div>
      <div>
        <b-textarea v-model="content"  placeholder="Tall textarea" rows="8"></b-textarea>
        <b-button @click="articleCreate()" >전송</b-button>
      </div>
    </form>
  </div>
</template>
 import axios from 'axios'
  export default {
    name:'ArticleCreate',
    data(){
      return{
        content:"",
        files: new FormData(),
      }
    },
    methods:{
      clickInputTag() {
        this.$refs['plus'].click()
      },
      uploadImage(event) { 
        console.log(event.target.files)
        const photoFile = document.getElementById("file")
        this.files.append("file", photoFile.files[0]);

        for (var image of event.target.files) {
          var reader = new FileReader(); 
          reader.onload = function(event) 
          { 
            var img = document.createElement("img"); 
            img.setAttribute("src", event.target.result); 
            img.setAttribute("width","25%"); 
            document.querySelector("div#image_container").appendChild(img); 
          }; 
          reader.readAsDataURL(image);
        } 
        console.log(this.files, typeof this.files)
      },
      articleCreate(){
        axios({
          url:'http://127.0.0.1:8080/article',
          method:'post',
          headers: {
            'x-access-token': `${localStorage.getItem('token')}`,
          },
          params: {
            content: this.content,
            files: this.files,
          }
        })
          .then(res=>{
            this.$router.push({ name:'article'})
            console.log(res.data)
            console.log(this.files)
          })
          .catch(err=>{
            console.log(`${localStorage.getItem('token')}`)
            console.log(this.files)
            console.log(this.content)
            console.log(err)
          })
      },
    }
  }

back背部

@PostMapping("/article")
    @ApiOperation(value = "게시글 작성")
    public Object postArticle(@RequestParam String content, @RequestParam(required = true) List<MultipartFile> files) throws IOException {
        Authentication user = SecurityContextHolder.getContext().getAuthentication();
        ResponseEntity response = null;
        if(user.getPrincipal() == "anonymousUser"){
            response = new ResponseEntity<>("Fail", HttpStatus.UNAUTHORIZED);
            return response;
        }else {
            UserDetails user2 = (UserDetails) user.getPrincipal();
            Optional<User> userOpt = userDao.findByEmail(user2.getUsername());

            Long articleId = articleDao.save(Article.builder()
                    .articleid(null)
                    .id(userOpt.get().getUid())
                    .createdtime(null)
                    .updatedtime(null)
                    .review(content)
                    .build()
            ).getArticleid();

            List<String> pathName = saveFiles(files);
            System.out.println(pathName.get(0));
            for(int i = 0; i < files.size(); ++i) {
                imageDao.save(Image.builder()
                        .imageid(null)
                        .articleid(articleId)
                        .imgURL(pathName.get(i))
                        .build()
                );
            }

            response = new ResponseEntity<>("게시글 작성 완료", HttpStatus.OK);
            return response;
        }
    }

Try adding content-type: multipart/form-data in header of your axios call.尝试在 axios 调用的标头中添加content-type: multipart/form-data

headers: {
 'x-access-token': `${localStorage.getItem('token')}`,
 'content-type': 'multipart/form-data'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 enctype=&quot;multipart/form-data&quot; 请求不起作用之后 - after enctype="multipart/form-data" request not working 如何在同一个jsp页面中查看jsp表单请求提交? (ENCTYPE =“多部分/格式数据”) - How can i check jsp form request submit in same jsp page? (enctype=“multipart/form-data”) 具有Multipart / Form-Data Enctype的表单验证失败后,Struts 1丢失请求参数 - Struts 1 Losing Request Parameters after Failed Form Validation with Multipart/Form-Data Enctype 使用 enctype=&quot;multipart/form-data&quot; 时请求 getParameter 始终为 null - request getParameter is always null when using enctype="multipart/form-data" 骆驼发送多部分/表单数据请求 - Camel send multipart/form-data request 使用 ByteArray 请求 MultiPart/Form-Data - Request MultiPart/Form-Data with ByteArray 使用 enctype=multipart/form-data 上传文件时出错,错误是上传的文件不是 multipart - error while file uploading using enctype=multipart/form-data , the error is the file bring uploaded is not multipart 如何修复请求不包含multipart / form-data或multipart / mixed stream apache错误 - how to fix the request doesn't contain a multipart/form-data or multipart/mixed stream apache error 请求不包含multipart / form-data或multipart / mixed stream apache错误 - the request doesn't contain a multipart/form-data or multipart/mixed stream apache error enctype multipart/form-data 在多数据类型表单中的使用 - Usage of enctype multipart/form-data in multi data type form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM