简体   繁体   中英

File Downloading in sails js

I am new to the sailsjs my problem is file was not downloading directly.It was storing in sailsjs app folder like this picture .I try a couple of Examples but file was not downloading.So please help me to how to over come this problem.If any plunger it will use full to me.Thank's a lot.

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



}

The above code is my controller.js file which is not downloading the file.But it was storing the file in my sails app as shown in above picture.

If you want to use this controller to download the file directly. You can use the skipper-disk package with sails https://www.npmjs.com/package/skipper-disk .

Then rather than

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

You could quickly install skipper-disk

npm install skipper-disk --save

Then change your code to this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM