Sails.js上传巨大的base64图像

时间:2015-01-24 13:32:44

标签: node.js base64 sails.js data-uri data-url

我有一个带输入的表单,其值是"data:image/jpg;base64,/9j xxxxxxx...".

之类的图像的数据URI

当用户使用此作品裁剪图像时,该值将被输入 - > http://fengyuanchen.github.io/cropper/ 所以在dragend事件中,我用getDataURL ...

填写输入值

在表单提交上,数据进入控制器,在那里我使用dataURI来获取扩展名,base64字符串等......

然后我将base64写入一个类似的文件:

fs.writeFile(fileRoot+filePathAndName, imageBase64, 'base64', function (err) { ...

一切都完美无瑕。但是......当我使用更大的图像(> 500kb,所以没那么大)时我得到了这个错误:

Unable to parse HTTP body- error occurred :: [Error: EUNFNTEX: Timed out waiting for known text parameters to finish streaming their bytes into the server.]

有趣的是,它始终适用于localhost :(即使是巨大的图像。

有谁知道如何才能使这项工作?或者甚至更好地建议如何使用船长或一个不错的服务来实现这一目标......

我在节点v0.10.33上使用SailsJS v0.10.5

2 个答案:

答案 0 :(得分:0)

它可能是很多东西......你的反向代理(如nginx或cloudflare)阻止了大量上传。正文解析器正在阻止大型urlencoded / json请求。

您应该做的是使用multipart或xhr2 / File API上传图像。

答案 1 :(得分:0)

我尝试了一切......客户端坚持让它成为客户端,我被困在了base64图像......所以我找到了一个hacky解决方案......

Inside sails app:node_modules / sails / node_modules / skipper / lib / Parser

我从:

更改了prototype.parseReq.js第167行
var ms = 5;

要:

var ms = 100;

现在它看起来像:

function finally_waitForTextParams() {
  // Careful: No error argument allowed in this callback!

  debug('waiting for any text params');

  // Make sure the `impatient` timeout fires no more than once
  clearTimeout(timer);

  // Take a look at all currently known text params for this Upstream,
  // then wait until all of them have been read.
  var ms = 100;
  var numTries = 0;
  async.doUntil(
    function setTimer(cb) {


      // Catch-all timeout, just in case something goes awry.
      // Should never happen, but a good failsafe to prevent holding on to
      // control forever.  It this timeout was to fire, we should error out and
      // cancel things.
      numTries++;
      if (numTries > 10) {
        return cb(new Error(
          'EUNFNTEX: Timed out waiting for known text parameters to finish ' +
          'streaming their bytes into the server.'
        ));
      }

      setTimeout(cb, ms);

      // Exponential backoff
      // (multiply ms by 2 each time, up to 500)
      ms = ms < 500 ? ms * 2 : ms;

    }

我知道这不是一个好的解决方案。如果有人有更好的想法,请帮助:)