【问题标题】:How to upload files with react to my .net server如何通过对我的 .net 服务器做出反应来上传文件
【发布时间】:2016-09-09 14:58:23
【问题描述】:

如何将图像上传到我的服务器。我正在使用 react,到目前为止,我的控制器看起来像这样:

 [Authorize]
 public object UploadAvatar()
{
            var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
}

当我使用 react 和 axios 上传时文件显示 = null:

const URL = '/api/userapi/uploadavatar';
var reader = new FileReader();
var file = e.target.files[0];
reader.readAsDataURL(file);
reader.onload = (upload) => {
  console.log(upload.target.result);
  axios.post(URL, {
    data: upload.target.result
  }, config);
}

但是当我用邮递员上传时它工作正常

【问题讨论】:

    标签: asp.net file reactjs axios


    【解决方案1】:

    您可以像这样发布文件对象:

    const URL = '/api/userapi/uploadavatar';
    var file = e.target.files[0];
    axios.post(URL, {data: file});
    

    【讨论】:

      【解决方案2】:

      在 React 中有几个组件可以上传文件,例如:

      http://okonet.ru/react-dropzone/

      https://github.com/lionng429/react-file-uploader

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2015-07-06
        相关资源
        最近更新 更多