2017-05-11 86 views
0

我是新來的sailsjs我的問題是文件沒有直接下載。它存儲在sailsjs應用程序文件夾like this picture。我嘗試了幾個例子,但文件沒有下載。所以請幫助我如何以克服這個問題。如果任何柱塞它將使用全部給我。謝謝了很多。文件下載在帆js

controller.js

downlodrentreceiptpdf:function(req,res){ 

     var rentData = req.param('data'); 

    sails.log('<<<<<<<-------------------UtilityController().downlodRentReceiptPdf-----------inside pdf method------>>>>>>'); 




var resultList = []; 
var date = new Date("10/14/2015"); 
var endDate = new Date("03/13/2016"); 


    sails.log('<<<<<<<-------------------UtilityController().downlodRentReceiptPdf-----------Testing 2------>>>>>>'); 


var months = endDate.getMonth() - date.getMonth() 
     + (12 * (endDate.getFullYear() - date.getFullYear())); 





     sails.log("months--->"+months) 

    var template=""; 



    template = template+ '<style>'+ 
    'html, body {'+ 
    'height: 100%;'+ 
    '}'+ 
    '#tableContainer-1 {'+ 
    'height: 100%;'+ 
    'width: 100%;'+ 
    'display: table;'+ 
    '}'+ 
    '#tableContainer-2 {'+ 
    'vertical-align: middle;'+ 
    'display: table-cell;'+ 
    'height: 100%;'+ 
    '}'+ 
    '#myTable > td,'+ 
    '#myTable > th{'+ 
    'margin: 0 auto;'+ 
    'border:1px solid black;'+ 
    '}'+ 
    '#myTable{'+ 
    'margin: 0 auto;'+ 
    '}'+ 
    '#tableth {'+ 
    'text-align:center;'+ 
'}'+ 
'</style>'+ 
'<div id=\"tableContainer-1\">'+ 
    '<div id=\"tableContainer-2\">'+ 
    '<table id=\"myTable\" style=\"border:1px solid black;border-collapse:collapse;\">'+ 
    '<tr >'+ 
    '<th colspan=\"2\" style=\"border:1px solid black;\">RENT RECEIPT</th>'+ 
    '</tr>'+ 
    '<tr border=\"0\" >'+ 
    '<td style=\"border:1px solid black;\">Tenant’s Name :</td>'+ 
    '<td style=\"border:1px solid black;\"> '+ rentData.name +' (Employee ID: '+rentData.employee+' ) </td>'+ 
    '</tr>'+ 
    '<tr border=\"0\">'+ 
    '<td style=\"border:1px solid black;\">Property Address :</td>'+ 
    '<td style=\"border:1px solid black;\">'+rentData.address+ 
'</td>'+ 
    '</tr>'+ 
    '<tr border=\"0\">'+ 
    '<td style=\"border:1px solid black;\">Rent Amount :</td>'+ 
    '<td style=\"border:1px solid black;\"> '+rentData.rent+' </td>'+ 
    '</tr>'+ 
    '<tr border=\"0\">'+ 
    '<td style=\"border:1px solid black;\">Period From :</td>'+ 
    '<td style=\"border:1px solid black;\">'+ rentData.pfrom +'</td>'+ 
    '</tr>'+ 
    '<tr border=\"0\">'+ 
    '<td style=\"border:1px solid black;\">Period To :</td>'+ 
    '<td style=\"border:1px solid black;\">'+ rentData.pto +'</td>'+ 
    '</tr>'+ 
    '</table>'+ 
    '</div>'+ 
'</div>' 



//sails.log("html template------->"+template); 



var options = { 
     "height": "5.5in", 
     "width": "8in", 
     "format": 'Letter', 
     // Page options 
     "border": { 
    "top": "2in",   // default is 0, units: mm, cm, in, px 
    "right": "1in", 
    "bottom": "2in", 
    "left": "1.5in", 
    "type": "pdf",    // allowed file types: png, jpeg, pdf 
    "quality": "75", 
    "directory": "/tmp" 
    },}; 


    htmltopdf.create(template, options).toFile('rentreceipt.pdf', function (err, file) { 

     if (err) { 
      return next(err); 
     } else { 
      console.log(file); 
      res.json({filepath: file.filename}); 
     } 
    }); 



} 

上面的代碼是未被下載file.But它被存儲在我的帆應用中的文件,如在上面的圖片我controller.js文件。

+0

哪裏是你的代碼? –

+0

請嘗試http://expressjs.com/en/4x/api.html#res.sendFile – Sangharsh

回答

0

如果您想使用此控制器直接下載文件。您可以使用風帆https://www.npmjs.com/package/skipper-disk的船長磁盤包。

然後而非

htmltopdf.create(template, options).toFile('rentreceipt.pdf', function (err, file) { 
    if (err) { 
    return next(err); 
    } else { 
    console.log(file); 
    res.json({filepath: file.filename}); 
    } 
}); 

你可以快速的安裝隊長盤

npm install skipper-disk --save 

然後更改您的代碼此

htmltopdf.create(template, options).toFile('rentreceipt.pdf', function (err, file) { 
if (err) { 
    return next(err); 
} else { 
    console.log(file); 

    // including skipper-disk 
    var SkipperDisk = require('skipper-disk'); 
    var fileAdapter = SkipperDisk(); 

    // using the fileadapter to pipe the response 
    fileAdapter.read(file.filename).on('error', function (err) { 
    return next(err); 
    }).pipe(res); 
    } 
});