简体   繁体   English

使用angularjs发布时文件始终为null

[英]file always null when post using angularjs

It works file when upload via postman but it didnt work when upload using anularjs 通过邮递员上传时可以工作,但是使用anularjs上传时却无法工作

.netcore controller .netcore控制器

[HttpPost, Route("csv/upload")]
public async Task<IActionResult> StudentCSV(IFormFile file)

html: 的HTML:

<div class="btn btn-rounded form-control">
 <input type="file"  class="input_file form-control" placeholder="select file">
</div>

angular controller: 角度控制器:

 var myfile = $('.input_file').prop('files');

  var formData = new FormData();
  formData.append("file", myfile);

  UniversityService.UploadCSV(formData).then(function (res) {
            console.log(res);
  });

angular service 角度服务

function UploadCSV(file) {

        CoreService.Post(url+ '/csv/upload/',file).then(function (response) {
            deferer.resolve(response);
        })
    }

the issue here is that file always null! 这里的问题是该file始终为空!

-You can use directive instead of mixing Jquery with Angularjs but to solve this issue change the upload function to something like this: -您可以使用指令,而不是将Jquery与Angularjs混合使用,但要解决此问题,请将上载函数更改为如下所示:

 $scope.upload = function () {

    var myfile = $('.input_file').prop('files');

    var formData = new FormData();
    formData.append("file", myfile[0]);// specify only one file 

    $http({
        method: 'POST',
        url: '/api/csv/upload',
        data: formData,
        headers: { 'Content-Type': undefined }// set contenct-type undefined
    }).then(function (result) {

    }, function (error) {

    })

}

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

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