我有以下代码,我想上传excel文件并保存到服务器。但是此功能不起作用,它向我显示500 (Internal Server Error)
我的代码有什么问题,请帮助我这个花了3个小时但没有成功的专业人员。我曾经是Google,但并非简单成功,我想通过angularjs或ajax调用
未从AJAX调用C#代码,我认为这就是其显示错误的原因
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);
}
}