我想使用AngularJS使用MVC4上传文件,我的文件使用以下代码成功上传到服务器但由于错误我无法获得确认。请检查我已完成的所有代码。
[HttpPost]
public JsonResult SaveFiles(string description)
{
string Message, fileName, actualFileName;
Message = fileName = actualFileName = string.Empty;
bool flag = false;
if (Request.Files != null)
{
var file = Request.Files[0];
actualFileName = file.FileName;
fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
int size = file.ContentLength;
try
{
string fPath = Server.MapPath("~/UploadedFiles/");
DirectoryInfo d = new DirectoryInfo(fPath);
if (!d.Exists)
{
d.Create();
}
file.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), fileName));
flag = true;
Message = "success";
}
catch (Exception)
{
Message = "File upload failed! Please try again";
}
}
return new JsonResult
{
Data = new
{
Message = Message,
Status = flag
}
};
}
var myApp = angular.module("myApp", []);
angular.module('myApp').controller('fileController', ["$scope", "FileUploadService", function ($scope, FileUploadService) {
//Save File
$scope.SaveFile = function () {
FileUploadService.UploadFile($scope.SelectedFileForUpload
, $scope.FileDescription
).then(function (d) {
console.log(d);
alert(d.Message);
ClearForm();
}, function (e) {
alert(e);
});
}
}])
.factory('FileUploadService', function ($http, $q) {
var fac = {};
fac.UploadFile = function (file, description) {
var formData = new FormData();
formData.append("file", file);
formData.append("description", description);
// debugger;
var defer = $q.defer();
try {
debugger;
$http.post("/Home/SaveFiles", formData,
{
withCredentials: true,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).success(function (data, status, headers, config) {
debugger;
console.log('success...');
defer.resolve(d);
}).error(function (data, status, header, config) {
debugger;
console.log('error...');
defer.reject("File Upload Failed!");
});
} catch (e) {
debugger;
console.log(e);
console.log(e.message);
}
return defer.promise;
}
return fac;
});
**TypeError**: Object doesn't support property or method 'success'
at fac.UploadFile (http://localhost:52240/myScript/Controller.js:92:13)
at $scope.SaveFile (http://localhost:52240/myScript/Controller.js:52:9)
at fn (Function code:2:138)
at callback (http://localhost:52240/Scripts/angular.js:26994:17)
at Scope.prototype.$eval (http://localhost:52240/Scripts/angular.js:18161:9)
at Scope.prototype.$apply (http://localhost:52240/Scripts/angular.js:18261:13)
at Anonymous function (http://localhost:52240/Scripts/angular.js:26999:17)
at r.event.dispatch (http://localhost:52240/Scripts/jquery-3.1.1.min.js:3:10259)
at q.handle (http://localhost:52240/Scripts/jquery-3.1.1.min.js:3:8269)
请帮忙......
提前致谢。
答案 0 :(得分:9)
如果您使用的是高于1.4的角度版本,则不推荐使用success
方法。使用then
来捕捉承诺。
$http.post("/Home/SaveFiles", formData,
{
withCredentials: true,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).then(function (response) {
debugger;
console.log('success...');
defer.resolve(response.data);
})
.catch(function (response) {
debugger;
console.log('error...');
defer.reject("File Upload Failed!");
});