1

私はIMGをアップロードすると、フォルダに保存するために、私のMVCコントローラで、以下のようにコードのこの部分を持っている:MVC、Angularjs、HttpPostedFileBase.SaveAsがページをリフレッシュしていますか?

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これは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)'と打つと、そのページがリロードを開始した後に気付きました。

誰でも私に説明することができますか?それ以降のページの再読み込みを防止する方法は?

答えて

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

}; 
+0

確かに、私は(COGのロード)のみのテストのために$タイムアウトを使用しています。 – Piosek

関連する問題