POST a file with React.js

后端 未结 2 1918
星月不相逢
星月不相逢 2021-01-25 16:52

The end user is supposed to upload through a file browser an excel file:



        
2条回答
  •  鱼传尺愫
    2021-01-25 17:22

    Passing a file to the backend is done via multipart/form-data forms.

    If you inspect the request that you send to the backend right now, you will see that your request does not send ( probably ) a multipart/form-data.

    You can check for reference What does enctype='multipart/form-data' mean? question.

    Your code should look something like :

    callUploadXLS: {
        remote( state, file ) {
                    // ^ make sure that `file` here is the same target.files[0]. It should be `File` Object
    
          // Will turn your form to `multipart/form-data`
          var data = new FormData();
          data.append('file', file);
    
          const url = `${base}/vendor/form`;
          return $http.instance.api.post( url, data );
    
        },
        success: Actions.XLSUploaded,
        error: Actions.fail
    }
    

提交回复
热议问题