简体   繁体   English

在 postman 中上传文件有效,但在 axios 中会引发 500 错误

[英]uploading file in postman works but in axios it throws 500 error

Guys my backend is working and i can send image file with form-data in post man but in my client side with axios i give a header of multipart/form-data and it throws {"statusCode":500,"message":"Internal server error"} in postman the content-type is:伙计们,我的后端正在工作,我可以在邮递员中发送带有表单数据的图像文件,但在我的客户端使用 axios 我给了一个 header 的 multipart/form-data 并且它抛出{"statusCode":500,"message":"Internal server error"} postman 中的{"statusCode":500,"message":"Internal server error"}内容类型为:

content-type: multipart/form-data; boundary=<calculated when request is sent>

my axios code:我的 axios 代码:

  PostNormalProductsFromServer(context,{formData,tokenSend}) {
const config = {
  headers: { 
            'Authorization': `${tokenSend}`,
            'Content-Type': 'multipart/form-data'
 }
 }
 axios.post('/api/product',addEnamel,config).then(response=>{
   if (response.status == 201) {
     alert(response.status)
     console.log(response.data)
     console.log(addEnamel.file)
   }
 }).catch(error => {
   if (error.response && error.response.status === 400) {
       alert("error.response")
   }
   else if(error.response && error.response.status === 401){
     console.log(tokenSend)
   }
 }) },

The data im sending to formData from component:我从组件发送到 formData 的数据:

var formData = new FormData();
formData.append("name", this.productname);
        formData.append("price", parseInt(this.price));
        formData.append("discount", parseInt(this.discount));
        formData.append("average_rating", this.way);
        formData.append("average_rating", 4);
        formData.append("texture", this.texture);
        formData.append("name_of_artist", this.artist);
        formData.append("time_for_artist_to_finish", parseInt(this.duration));
        formData.append("weight", parseInt(this.weight));
        formData.append("height", parseInt(this.height));
        formData.append("width", parseInt(this.width));
        formData.append("length", parseInt(this.length));
        formData.append("usage_of_product", this.usages.join());
        formData.append("type_of_colors_used", this.color);
        formData.append("washable", Boolean(this.wash));
        formData.append("can_be_heated", this.categoryCode);
        formData.append("description", this.extra);
        formData.append("category", Boolean(this.heat));
        formData.append("file", this.pictures[0]);
 this.$store.dispatch("PostNormalProductsFromServer", {formData,tokenSend});

guys I'm sending easily with postman where the body is in form-data.伙计们,我很容易用 postman 发送身体在表单数据中。 What is the problem with my axios code?我的 axios 代码有什么问题?

You probably have a 'CORS' error您可能遇到“CORS”错误

Add this code to your backend source (Top of the routes):将此代码添加到您的后端源(路线顶部):

 const cors = require('cors'); app.use(cors());

then run this in terminal:然后在终端中运行它:

npm i cors npm i cors

This snippet code works for me这个片段代码对我有用

var form = new FormData();
var file = document.querySelector('#file');
form.append("image", file.files[0]);
axios.post('upload_file', form, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM