本文介绍了如何将文档文件转换为角度js中的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个web api,它接受一个文件作为字节数组,然后以数据库中的字节数组的形式存储文件。
所以我想从UI中选择一个文件然后将其转换为字节数组并将其发送到后端,如何将文件转换为角度js中的字节数组。
我尝试了什么:
i wrote a web api where it accepts a file as a byte array and then stores the file in the form of a byte array in the database.
so i want to select a file from UI and then convert that into a byte array and send it to the backend,how can i convert file into byte array in angular js.
What I have tried:
var myApp = angular.module('myApp', []);
myApp.directive('fileModel', ['$parse', function ($parse) {
alert(2);
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function () {
scope.$apply(function () {
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
myApp.service('fileUpload', ['$http', function ($http) {
alert(3);
this.uploadFileToUrl = function (file, uploadUrl) {
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.success(function () {
})
.error(function () {
});
}
}]);
myApp.controller('myCtrl', ['$scope', 'fileUpload', function ($scope, fileUpload) {
$scope.uploadFile = function () {
alert(1);
var file = $scope.myFile;
console.log('file is ');
console.dir(file);
var uploadUrl = "/api/uploadDoc/";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
推荐答案
这篇关于如何将文档文件转换为角度js中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!