POST a file with React.js

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

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



        
相关标签:
2条回答
  • 2021-01-25 17:17

    Try to use good file uploader and inspect element check your network tab whether you api call that is file upload is sending proper file data as expected by the server. Test the API from Postman if its working then it means front-end code has problem and you can compare the headers what wrong items you are sending.

    Hope this debugging method will help you to recognize your mistake.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题