0
文件夾
我的代碼看起來像
var myApp = angular.module('myApp', []);
myApp.directive('fileModel', ['$parse',
function($parse) {
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) {
this.uploadFileToUrl = function(file, uploadUrl) {
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': 'application/json'
}
})
.success(function() {})
.error(function() {});
}
}
]);
myApp.controller('myCtrl', ['$scope', 'fileUpload',
function($scope, fileUpload) {
$scope.uploadFile = function() {
var file = $scope.myFile;
console.log('file is ');
console.dir(file);
var uploadUrl = "http://localhost:22729/Data/";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<input type="file" file-model="myFile" />
<button ng-click="uploadFile()">upload me</button>
</div>
</body>
的web.config文件看起來像這樣
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
我試過很多備選方案,仍然面臨這個問題。
另外我想知道其他文件類型是如何上傳的。
我想要文件只上傳到本地文件夾 – Aravind
我會引用另一個線程結束了相同的答案:http://stackoverflow.com/questions/8232033/use-http-post-to-send-file-to-iis-7-5-virtual-directory必須有某種形式的代碼在服務器端捕獲請求並將文件從請求主體複製到所需的位置。 – milope