Angular Ng-File-Upload url 找不到 MVC ApiController 方法
Angular Ng-File-Upload url cannot find MVC ApiController Method
我正在尝试使用 ng-file-upload 上传文件并将 blob 保存到数据库,就像我之前的问题
我相信我有 "correct" 代码来完成任务,但现在的问题是我无法获取 "hit" ApiController 中方法的代码。如果我将该方法放在常规异步控制器中,它会找到它(但我无法使用所需代码的一半,因为它不起作用,除非它位于从 ApiController 继承的控制器中),但它不会在 ApiController 中找到相同的方法。我错过了什么?
这里是 Angular 控制器代码:
angular.module('myModule').controller('DocumentCtrl', function ($scope, $interval, $window, $filter, $q, $timeout, Upload, $http) {
$scope.uploadPic = function (file) {
file.upload = Upload.upload({
url: '/SSQV4/SSQV5/Document/UploadEMRDocument',
method: 'POST',
data: { file: file }
})
}
})
这是 MVC 控制器代码:
public class DocumentController : ApiController
{
public async Task<IHttpActionResult> UploadEMRDocument()
{
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
}
我假设这是某种路由问题,但我不确定。非常感谢任何帮助!
我需要将 url 更改为
url: '/SSQV4/SSQV5/api/Document/UploadEMRDocument'
现在它成功了。感谢您的帮助 Nava-Prev.Queti!
我正在尝试使用 ng-file-upload 上传文件并将 blob 保存到数据库,就像我之前的问题
我相信我有 "correct" 代码来完成任务,但现在的问题是我无法获取 "hit" ApiController 中方法的代码。如果我将该方法放在常规异步控制器中,它会找到它(但我无法使用所需代码的一半,因为它不起作用,除非它位于从 ApiController 继承的控制器中),但它不会在 ApiController 中找到相同的方法。我错过了什么?
这里是 Angular 控制器代码:
angular.module('myModule').controller('DocumentCtrl', function ($scope, $interval, $window, $filter, $q, $timeout, Upload, $http) {
$scope.uploadPic = function (file) {
file.upload = Upload.upload({
url: '/SSQV4/SSQV5/Document/UploadEMRDocument',
method: 'POST',
data: { file: file }
})
}
}) 这是 MVC 控制器代码:
public class DocumentController : ApiController
{
public async Task<IHttpActionResult> UploadEMRDocument()
{
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
}
我假设这是某种路由问题,但我不确定。非常感谢任何帮助!
我需要将 url 更改为
url: '/SSQV4/SSQV5/api/Document/UploadEMRDocument'
现在它成功了。感谢您的帮助 Nava-Prev.Queti!