我正在使用nervgh的Angular-File-Upload https://github.com/nervgh/angular-file-upload
当我对preOpKey
进行硬编码时,它就像一个魅力。我想要做的是发送preOpKey
和文件,以便我可以将文件保存到数据库中的相应记录。
angular-file-upload在API中有一个fileData
,我在$scope.OnPreinspectionSubmit()
中填充但由于某种原因,我在MVC控制器中找不到SaveFile()
的值被称为。
我只需要知道如何将值与我的文件一起传递,然后在我的MVC控制器中访问它。我想在fileData
发送它会很方便,但这不是回答我的问题所必需的。
这是我的Angular控制器:
var OperatorPreinspectionControllers = angular.module('OperatorPreinspectionControllers', ['angularFileUpload']);
OperatorPreinspectionControllers.controller('OperatorPreinspectionCtrl', ['$scope', '$http', 'FileUploader',
function ($scope, $http, FileUploader) {
$scope.uploader = new FileUploader({
url: pageBaseUrl + 'image/SaveFile'
});
$scope.uploader.filters.push({
name: 'imageFilter',
fn: function (item /*{File|FileLikeObject}*/, options) {
var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
return '|jpg|png|jpeg|bmp|gif|pdf|'.indexOf(type) !== -1;
}
});
// Send the form data to the database
$scope.OnPreinspectionSubmit = function () {
if (confirm("Are you sure you want to save this information?")) {
$http.post(pageBaseUrl + 'api/PreInspectionForm', $scope.formInformation).success(function (returnData) {
$scope.uploader.formData.push({ preOpKey: returnData });
$scope.uploader.uploadAll(); // Upload file
});
} else { }
}
}
]);
这是我的MVC控制器:
public void SaveFile()
{
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase uploadedFile = files[0];
var preOpKey = 123; // ???????
// Turn the file into bytes
byte[] data;
using(Stream inputStream = uploadedFile.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if(memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
PreOpManager.SaveImage(preOpKey, data, uploadedFile.FileName, uploadedFile.ContentType);
}
谢谢,
亚伦
答案 0 :(得分:11)
到目前为止,对我来说完美无缺的解决方案一直是:
$scope.uploader.onBeforeUploadItem = onBeforeUploadItem;
function onBeforeUploadItem(item) {
item.formData.push({your: 'data'});
console.log(item);
}
根据https://github.com/nervgh/angular-file-upload/issues/97#issuecomment-39248062