MVC, Angularjs, HttpPostedFileBase.SaveAs is refreshing page?

I have this piece of code as below in my MVC controller to upload img and to save it in folder:

    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, ""); // :(
    }

A this is my code in angularjs service:

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,
}

}]);

And what can I say, it works great, but why after executing this code, entire view is refreshing?

I dont have any refreshing or view changing functions in my angular controller.

When I'm debuging code, and I hit break point in line: 'file.SaveAs(path)' I noticed that after that page starts to reloading itself.

Could anyone explain to me why? How to prevent reloading page after that?

2 answers

  • answered 2017-06-17 18:34 Piosek

    Here Solution.

    My bad. After small resreach I found it: here

  • answered 2017-06-17 18:34 Arun

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