jquery - 上传excel文件webapi和angularjs和ajax调用
问题描述
我有以下代码我想上传 excel 文件并保存到服务器。但是这个功能不起作用它告诉我 500 (Internal Server Error)
我的代码有什么问题请帮助我这个任何专业人士我花了 3 个小时但没有成功。我是谷歌但不是成功简单我想通过 angularjs 或 ajax 调用上传文件
C# 代码不从 AJAX 调用我认为这就是它显示错误的原因
HTML
<input type="file" id="uploadInput" />
<button ng-click="SubmitForm()" class="btn btn-info">Submit</button>
AppModel.controller('ImportExcelCtrl', ['$scope', '$filter', '$http', function ($scope, $filter, $http) {
$scope.SubmitForm = function () {
var element = document.getElementById("uploadInput");
var file = element.files[0];
console.log(file.name);
console.log(file);
var loFormData = new FormData();
loFormData.append("filename", file.name);
loFormData.append("file", file);
var loAjaxRequest = $.ajax({
cache: false,
type: 'POST',
url: "/api/myapi/UploadFileExcel",
contentType: false,
processData: false,
data: loFormData,
headers: {
'Content-Type': 'multipart/form-data'
}
});
loAjaxRequest.done(function (xhr, textStatus) {
alert(textStatus);
});
};
}]);
CS
[HttpPost]
public async Task<HttpResponseMessage> UploadFileExcel()
{
if (!this.Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
try
{
var loProvider = new MultipartFormDataStreamProvider(Path.GetTempPath());
await Request.Content.ReadAsMultipartAsync(loProvider);
string lsFilename = loProvider.FormData.GetValues("filename").First();
var loFile = loProvider.FileData.First();
string lsFileContent = File.ReadAllText(loFile.LocalFileName);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (Exception exp)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
解决方案
推荐阅读
- mysql - 从另一个表更新列后mysql触发器
- android - 需要识别视图类型
- html - 如何在 CSS 中为 background:url(" ") 图像添加深色叠加而不影响文本?
- asp.net-core - 如何使用 EF 以实体的形式获取嵌套查询的结果
- tensorflow - 在张量流中进行优化
- ruby-on-rails - 如何创建由 Ruby on Rails 支持的用户/事件跟踪工具
- enums - 给定保留字段标签的 proto2 中的默认枚举值是多少?
- git - 推送不属于我的 github
- python - Python乘法,对不同长度的数组求和:优化代码
- django - Django-filter 和 Django-tables2 过滤器,ModelChoice 值未过滤