2017-05-11 14 views
0

クライアント側でAngularまたはJavascriptを使用してファイルを作成し、サーバーに送信します。 MVCを使用して は私のサーバー機能をコントローラクライアント側でファイルを作成してサーバーに送信

public void SavePivotFile(HttpPostedFileBase file) 
    { 
     try 
     { 
      if (file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~"), System.Configuration.ConfigurationManager.AppSettings["reportsFolder"].ToString(), fileName); 
       file.SaveAs(path); 
      } 
     } 
     catch(Exception e) 
     { 
      throw; 
     } 
    } 

は今、私のクライアント側では、私は、ファイルのようにSavePivotFileに送信したいオブジェクトを持っています。私はこれを試みたが、doesntの仕事。オブジェクト 'options'はJSONです。

  $http({ 
       method: 'GET', 
       url: '/FileManager/SavePivotFile', 
       params: { 
        file: options, 
       } 
      }).then(function successCallback(response) { 
       showNotification('The changes have been saved.', 'info'); 
      }, function errorCallback(response) { 
       showNotification('Failed to save the file.', 'error'); 
      }); 

また、送信前に新しいFormData()を作成しようとしましたが、動作しません。どのように猫はオプションのJSONオブジェクトを取って、ファイルのようなサーバーに渡す?

答えて

0
//C# Code 
    [HttpPost] 
    [Route('FileManager/SavePivotFile')] 
    // you can use [Allow(Role)] to allow particular role. Google it! 
    public void SavePivotFile(HttpPostedFileBase file) 
{ 
    try 
    { 
     if (file.ContentLength > 0) 
     { 
      var fileName = Path.GetFileName(file.FileName); 
      var path = Path.Combine(Server.MapPath("~"), System.Configuration.ConfigurationManager.AppSettings["reportsFolder"].ToString(), fileName); 
      file.SaveAs(path); 
     } 
    } 
    catch(Exception e) 
    { 
     throw; 
    } 
} 



//Angular Code 
$http.post('FileManager/SavePivotFile',options)//Optionsistheobjectuwanttosend 
     .success(function(res){ 
     //your code. since the c# method isvoid you will not get any response 
     }) 
     .error(function(e){ 
     //your error handling 
     }) 

HttpPostedFileBaseモデルはオプションと似ています。そうすれば、C#でJSONにアクセスできます。

これが動作するかどうかを教えてください。

関連する問題