2016-10-03 2 views
0

원격 메서드를 사용하여 동적으로 생성 된 pdf 파일을 다운로드하려는 경우 해당 파일이 특정 경로에 존재하며 반환 유형 "파일"을 사용하고 있습니다. 내 구현은 다음과 같습니다원격 메서드를 사용하여 pdf 다운로드

customer.downloadFile = function downloadFile(userId, cb){ 
     var reader = fs.createReadStream(__dirname + '/../document.pdf'); 
     cb(null, reader, 'application/pdf'); 
    }; 

    customer.remoteMethod(
     'downloadFile', 
     { 
      isStatic: true, 
      accepts: [ 
       {arg: 'id', type: 'string', required: true} 
        ], 
      returns: [ 
       { arg: 'body', type: 'file', root: true }, 
       { arg: 'Content-Type', type: 'string', http: { target: 'header' } } 
      ], 
      http: {path: '/:id/downloadFile', verb: 'get'} 
     } 
    ); 

위의 코드 문제는 아름다운 pdf 파일 컨테이너 대신 파일에 다음과 같은 오류의 표시하지만 브라우저가 표시되고 있다는 점이다 :

enter image description here

지적하십시오 코드의 문제점과 수정 방법에 관해서 이 url에서 리드를 얻으십시오 : https://github.com/strongloop/loopback-swagger/issues/34

+0

내용 - 처리 : 인라인; filename = "filename.pdf"// 헤더에 들어가기 – MMK

답변

0

이있어 그 다음 작업 :

fs.readFile(fileName, function (err, fileData) { 
    res.contentType("application/pdf"); 
    res.status(200).send(fileData); 

    if (!err) { 
     fs.unlink(fileName); 
    } 
    else { 
     cb(err); 
    } 
}); 
+1

여기서'res'가 정의되어 있습니까? – user3568719

1

다운로드 가능한 파일을 관리하려면 loopback-component-storage을 사용해야합니다.

파일은 소위 컨테이너 (기본적으로 계층 구조가 단일 수준이거나 그 이상이 아닌 폴더)로 그룹화됩니다.

은 그것을 수행하는 방법 :

  1. 는 예를 들어 container라는 모델을 생성합니다.
  2. connectorloopback-component-storage
  3. 이 데이터 소스를의 storage

에게 container 모델에 부여 사용하는 storage 데이터 소스를 만듭니다. 컨테이너에 파일을 업로드하거나 컨테이너에서 파일을 다운로드 할 수 있습니다. 컨테이너를 사용하면 파일을 로컬 파일 시스템에 저장하거나 나중에 Cloud 솔루션으로 이동할 수 있습니다.