Aar*_*zar 10 javascript asp.net-mvc angularjs
我在https://github.com/nervgh/angular-file-upload上使用nervgh的Angular-File-Upload
当我硬编码时,它就像一个魅力preOpKey.我想要做的是发送preOpKey文件以便我可以将文件保存到数据库中的相应记录.
angular-file-upload fileData在我填充的API中有一个,$scope.OnPreinspectionSubmit()但由于某种原因,我无法SaveFile()在我的MVC控制器中找到该值.
我只需要知道如何传递一个值和我的文件,然后在我的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 { }
}
}
]);
Run Code Online (Sandbox Code Playgroud)
这是我的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);
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
亚伦
Sci*_*tes 11
到目前为止,对我来说完美无缺的解决方案是:
$scope.uploader.onBeforeUploadItem = onBeforeUploadItem;
function onBeforeUploadItem(item) {
item.formData.push({your: 'data'});
console.log(item);
}
Run Code Online (Sandbox Code Playgroud)
根据https://github.com/nervgh/angular-file-upload/issues/97#issuecomment-39248062
| 归档时间: |
|
| 查看次数: |
10635 次 |
| 最近记录: |