使用multer上载XML文件

时间:2019-09-01 22:06:22

标签: javascript express multer

我有以下代码生成一个XML文件,并使用nodejs文件系统(fs)模块将其保存在应用程序目录中。是否可以使用multer库保存生成的XML?

中间件xmlFile.js

var_export()

server.js

const builder = require('xmlbuilder')
const clientMocks = require('../mocks/clients')
const fs = require('fs')
const path = require('path')

// Route file saved!
const dirPath = path.join(__dirname, '../../public', 'xmlfiles', 'clients.xml')

function xmlbuildFile (req, res, next) {
  const xml = builder
    .create('clients')
    .dec('1.0', 'UTF-8')
    .doc()

  for (let i = 0; i < clientMocks.length; i++) {
    xml.ele('client')
      .ele('id_tax_number', clientMocks[i].id_tax_number).up()
      .ele('business_name', clientMocks[i].business_name).up()
      .ele('phone', clientMocks[i].address).up()
      .ele('address', clientMocks[i].address).up()
      .ele('email', clientMocks[i].business_name).up()
      .ele('image', clientMocks[i].image).up()
  }

  // convert XML document to a String
  const xmldoc = xml.toString({ pretty: true })
  fs.writeFile(dirPath, xmldoc, function (err) {
    if (err) { return console.log(err) }
    console.log('The file was saved!')
  })
  next()
}


module.exports = xmlbuildFile

0 个答案:

没有答案