【问题标题】:Upload File and Data with Angularjs in MVC .net core在 MVC .net 核心中使用 Angularjs 上传文件和数据
【发布时间】:2021-09-19 14:01:31
【问题描述】:
<input type="file" ng-model="ch.FileName" id="file" name="myFile" nchange="angular.element(this).scope().saveChapter(this.files)"/>
Action Method,Js and other Code
【问题讨论】:
标签:
angularjs
file-upload
asp.net-core-mvc
angularjs-fileupload
【解决方案1】:
在 MVC .net core 中使用 Angularjs 上传文件和数据
如果您想将带有其他附加信息/数据的二进制数据从您的 angularjs 前端发布到 WebAPI 后端,您可以尝试通过FormData object 传递数据,如下所示。
var formdata = new FormData();
formdata.append('chapterID', $scope.ch.ChapterID);
formdata.append('bookID', $scope.ch.BookID);
formdata.append('chapterName', $scope.ch.FileName);
//...
formdata.append('cHFile', $scope.chfile);
$http({
method: 'POST',
url: 'your_request_url_here',
headers: { 'Content-Type': undefined },
data: formdata
}).then(function mySuccess(response) {
//...
在浏览器开发者工具Network tab中,可以找到按预期传递的数据
测试结果