2017-12-14 2 views
0

내가 그물 코어 웹 API와 각도와 aspnetboilerplate 템플릿에서 안전한 파일 다운로드를 개발하기 위해 노력하고 안전한 파일</p> <p>2.0 내가 Web.host이을에 시도했지만이 API로 치료되지 않는 다운로드

public class DownloadController : ProjectControllerBase 
    { 
     private IHostingEnvironment _env; 
     public FileResult DownloadYourFile() 
     { 
      try 
      { 
       long uid = (AbpSession.UserId == null) ? 0 : Convert.ToInt64(AbpSession.UserId); 
       var net = new System.Net.WebClient(); 
       string path = Path.Combine(_env.WebRootPath, "downloads/xyz.doc");          
       var data = net.DownloadData(path); 
       var content = new System.IO.MemoryStream(data); 
       var contentType = "APPLICATION/octet-stream"; 
       var fileName = "xyz.doc"; 
       return File(content, contentType, fileName); 
      } 
      catch (Exception e) 
      { 
       throw new UserFriendlyException("Error", "Error downloading file. Please try again.", e); 
      }    
     } 

    } 

내가 Web.Application 층에서이 작업을 수행 할 수 있습니다

+0

'API로 취급하지 않음'이란 무엇을 의미합니까? – aaron

+0

@aaron swagger에 표시되지 않습니다. 어떻게하면 web.application 어떤 아이디어에 쓸 수 있습니다 – Nighil

+0

Swagger에 나타나면 충분합니까? 응용 프로그램 계층에서이를 수행하는 것이 사소하지도 않으며 권장되지도 않습니다. 실제로이를 수행하는 데 실제적인 이점은 없습니다. – aaron

답변

0

자신감에 새로운 방법을 표시하려면, 당신이 할 수있는 방법은 다음과 같습니다

[Route("api/[controller]/[action]")] //Should add route config for your controller 
public class TestController : AbpCoreControllerBase 
{ 
    [HttpGet] //Should add HttpMethod to your method 
    public string TestMethod() 
    { 
     return "Hello world"; 
    } 
} 

가 파일 다운로드를 확보하기 위해, 당신이 할 수 있다고 생각 : 당신이 권한을 확인해야 할 경우

  • 이 컨트롤러/방법

  • 를 주입 PermissionChecker에 AbpAuthorize 특성을 추가하여 수동으로

가져 오기 자세한 내용 here

0

@tiennguyen 대답은 정확합니다.

Swagger는 틀에 얽매이지 않는 메서드 이름을 표시하지 않습니다. 따라서 [Route] 특성을 작성해야합니다. Swagger에 등재되는 것은 crutial이 아닙니다. 당신은 행동을 부를 수 있습니다.

[AbpMvcAuthorize] 
[Route("api/[controller]/[action]")] 
public class DownloadController : ProjectControllerBase 
{   
    public FileResult DownloadYourFile() 
    { 
      ....  
    }  
} 
관련 문제