.net MVC、Angularjs、HttpPostedFileBase.SaveAs正在刷新页面?
我的MVC控制器中有以下代码,用于上传img并将其保存在文件夹中:.net MVC、Angularjs、HttpPostedFileBase.SaveAs正在刷新页面?,.net,angularjs,model-view-controller,uploading,httppostedfilebase,.net,Angularjs,Model View Controller,Uploading,Httppostedfilebase,我的MVC控制器中有以下代码,用于上传img并将其保存在文件夹中: public ActionResult UploadLogo(HttpPostedFileBase file) { var path = ""; if (file != null) { if (file.ContentLength > 0) { if (Path.GetExt
public ActionResult UploadLogo(HttpPostedFileBase file)
{
var path = "";
if (file != null)
{
if (file.ContentLength > 0)
{
if (Path.GetExtension(file.FileName).ToLower() == ".jpg" ||
Path.GetExtension(file.FileName).ToLower() == ".png" ||
Path.GetExtension(file.FileName).ToLower() == ".gif" ||
Path.GetExtension(file.FileName).ToLower() == ".jpeg")
{
path = Server.MapPath("~/GroupApp/Content/Images/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = Path.Combine(path, file.FileName);
file.SaveAs(path);
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
}
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ""); // :(
}
这是我在angularjs服务中的代码:
groupApp.factory('uploadLogo', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
var _uploadLogo = function (file) {
var deferred = $q.defer();
var controllerQuery = "groupApi/uploadLogo";
$timeout(function () {
deferred.notify(true);
}, 0)
var _file = new FormData();
_file.append('file', file, file.name);
$http.post(controllerQuery, _file, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined },
enctype: 'multipart/form-data'
})
.then(function (result) {
$timeout(function () {
deferred.resolve(result);
}, 200);
},
function (error) {
deferred.reject(error);
});
return deferred.promise;
};
return {
uploadLogo: _uploadLogo,
}
}]);
我能说什么呢,它工作得很好,但为什么在执行了这段代码之后,整个视图都在刷新呢
我的角度控制器中没有任何刷新或视图更改功能
当我调试代码时,我在“file.SaveAs(path)”行中遇到了断点,我注意到在该页开始重新加载自己之后
谁能给我解释一下原因吗?如何防止在此之后重新加载页面?
我的错。经过小范围的重新接触后,我发现:
我的错。经过小范围的重新接触后,我发现:
当然,我使用$timeout仅用于测试(加载cog)。当然,我使用$timeout仅用于测试(加载cog)。
Can you remove the timeout function and access the result directly.
Like this
$http.post(controllerQuery, _file, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined },
enctype: 'multipart/form-data'
})
.then(function (result) {
deferred.resolve(result);
},
function (error) {
deferred.reject(error);
});
return deferred.promise;
};