2017-05-11 9 views
0

클라이언트 측에서 Angular 또는 Javascript를 사용하여 파일을 만들어 서버로 보내려고합니다. MVC 내 서버 기능을 컨트롤러 사용 내 클라이언트 측에서, 내가 파일처럼 SavePivotFile에 보낼 객체가 지금클라이언트 측에서 파일 만들기 및 서버에 보내기

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

입니다. 나는 이것을 시도했지만 나던 일을하지 않았다. '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에 액세스 할 수 있습니다.

이 기능이 작동하는지 알려주세요.

관련 문제