使用node.js将tiff转换为png

时间:2018-11-02 09:59:17

标签: node.js angular npm mean-stack

我正在使用MEAN Stack和Angular 6实现一个Web应用程序。我想在其中选择文件扩展名为'.tiff'的地图。我想将其转换为“ .png”格式,然后将“ .png”文件上传到名为“ uploads”的单独文件夹中。 这是我的路线。

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var multer = require('multer');
var ConvertTiff = require('tiff-to-png');

var mapInfo = require('../../../models/mongoModels/map');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now() + '.png')
  }
})

var upload = multer({ storage: storage });
router.post('/uploadMap', upload.single('milespecMap'), function (req, res, next) {
  var imageName = req.file.filename;
  var options = {
    logLevel: 1
  };
  var converter = new ConvertTiff(options);
  var location = '';
  converter.convertOne(req.file.originalname+'.tif', location);
  res.send(imageName);

});


//Save or update map 

router.post("/update", function (req, res) {
      var mod = new mapInfo(req.body);
      mapInfo.findOneAndUpdate(
        {
          mapName: req.body.mapName,
        },
        req.body,
        { upsert: true, new: true },

        function (err, data) {
          if (err) {
            console.log(err);
            res.send(err);
          } else {
            // console.log(res);
            res.send(mod);
          }
        }
      );
    });

  module.exports = router;

这是我的.ts文件

export class MapsComponent implements OnInit {
  closeResult: string;
  currentMapDetails: Map = new Map();
  selectedFile: File = null;
  imageName: string;




  public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'milespecMap', });
  private items: any;
  constructor(private modalService: NgbModal,
    private mapsService: MapsService,
    private http: Http,
    private router: Router,

  ) {
    this.uploader.onAfterAddingAll(this.items)
    {
      console.log(this.items);
    }

    this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
      //things to do on completion
      this.imageName= response;
      console.log("image"+ " "+response);
    };

  }

  ngOnInit() {
  }
  openModal(content: any) {
    this.modalService.open(content).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }

  handleFileInput(file: FileList) {
    this.selectedFile = file.item(0);
    var reader = new FileReader();
    reader.onload = (event: any) => {
      this.currentMapDetails.milespecUrl = event.target.result;
      console.log(reader.onload);
    }
    reader.readAsDataURL(this.selectedFile);

  }

  updateInfo() {
    this.uploader.uploadAll(); ///image upload
    this.update();
  }

  update() {
    console.log(this.currentMapDetails);
    this.mapsService.updateMap(this.currentMapDetails).subscribe(res => {
      this.currentMapDetails;
      console.log(res);
    }, (err) => {
      console.log(err);
    });
    console.log(this.currentMapDetails);

  }

  retriveLocationOfTiff() {

  }
}

有人可以帮助我获取该位置的文件路径吗?

0 个答案:

没有答案