gpt4 book ai didi

javascript - 对象不支持 AngularJS 中的属性或方法 'success'

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:56 24 4
gpt4 key购买 nike

我想使用带 AngularJS 的 MVC4 上传文件,我的文件使用以下代码成功上传到服务器,但由于错误我无法获得确认。请检查我已经完成的所有代码。

我的 C# Controller 代码

[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
}
};
}

模块.js

  var myApp = angular.module("myApp", []);

Controller .js

    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)

请帮忙...

提前致谢。

最佳答案

如果您使用的是高于 1.4 的 Angular 版本,则 success 方法已弃用。使用 then 来捕捉 promise 。

$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!");
});

关于javascript - 对象不支持 AngularJS 中的属性或方法 'success',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44653521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com