0

我正在尝试上传一个文件并使用 fontforge 将其转换,以将其发送回用户,所有这些都非常强大。我的代码在我的第一台机器上运行,但随后我创建了一个带有 Vagrant 的 Ubuntu-Server VM,在其上使用 SSH,DLed fontforge,强大(基本上是 nodejs),然后重试。这就是我的server.js文件的样子:

var formidable = require('formidable'),
http = require('http'),
exec = require("child_process").exec,
fs = require("fs");


http.createServer(function(req, res) {

    var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
    console.log(files);
    console.log(fields);
    exec("fontforge -script convert.sh -format \"." + fields.format + "\"" + " " + files.upload.path, function(error, stdout, stderr) {
        if (error !== null) {
          console.log('exec error: ' + error);
        }
        res.setHeader('Content-disposition', 'attachment; filename=' + files.upload.name.replace('svg', fields.format));
        res.writeHead(200, {'Content-Type': 'application/x-font-' + fields.format});
        res.end(files.upload.path + "." + fields.format);
        fs.unlink(files.upload.path, function (err) {
          if (err) throw err;
          console.log('successfully deleted ' + files.upload.path);
        });
        fs.unlink(files.upload.path + "." + fields.format, function (err) {
          if (err) throw err;
          console.log('successfully deleted ' + files.upload.path + "." + fields.format);
        });

      });
    });

    return;
}).listen(8080);

表单在客户端上正确显示(一些带有 post 方法的基本 index.html 页面),但给我发回了一个 31 字节的文件(而不是 6.1k),具有正确的名称和扩展名,但绝对不是正确的内容。

控制台日志显示:

{ upload:
   { domain: null,
    _events: {},
     _maxListeners: 10,
 size: 29526,
 path: '/tmp/bcb357fa7f8e5fcc075964c6bcbbe9bb',
 name: 'hamburg.svg',
 type: 'image/svg+xml',
 hash: null,
 lastModifiedDate: Mon Jul 21 2014 11:04:00 GMT+0000 (UTC),
 _writeStream:
  { _writableState: [Object],
    writable: true,
    domain: null,
    _events: {},
    _maxListeners: 10,
    path: '/tmp/bcb357fa7f8e5fcc075964c6bcbbe9bb',
    fd: null,
    flags: 'w',
    mode: 438,
    start: undefined,
    pos: undefined,
    bytesWritten: 29526,
    closed: true } }     }

{ format: 'ttf' }
4

1 回答 1

0

您的问题是您正在发送转换后文件的路径。要发送文件本身,您需要使用:

fs.readFile(files.upload.path + '.' + fields.format, function (err, data) {
    if (err) {
        res.end('Error: ' + err);
    } else {
        res.end(data);
    }
});
于 2014-07-21T15:35:39.727 回答