简体   繁体   English

Dropzone 客户端通过文件上传到 AWS 预签名 url 调整大小

[英]Dropzone client side resize with file upload to AWS pre-signed url

I implemented image file upload to an AWS s3 bucket with dropzone.我使用 dropzone 将图像文件上传到 AWS s3 存储桶。 It creates a pre-signed url and ultimately sends the image file to s3.它创建一个预签名的 url,并最终将图像文件发送到 s3。 I cannot get this to work in combination with the client side image resizing dropzone provides.我无法将其与调整 dropzone 提供的客户端图像大小结合使用。

For s3 upload to work properly, I had to override sending , as explained here https://github.com/enyo/dropzone/issues/590 .为了使 s3 上传正常工作,我必须覆盖sending ,如此处所解释的https://github.com/enyo/dropzone/issues/590 Otherwise, it would send form data and I would get the image wrapped in ------WebKitFormBoundary...否则,它会发送表单数据,我会将图像包装在------WebKitFormBoundary...

This, from the link above is working:这个,从上面的链接工作:

  sending: function(file, xhr) {
    var _send = xhr.send;
    xhr.send = function() {
      _send.call(xhr, file);
    };
  }

However, I get problems when I try to combine the above approach with dropzone's client side image resize.但是,当我尝试将上述方法与 dropzone 的客户端图像调整大小相结合时,我遇到了问题。 As per the dropzone documentation, I specified:根据 dropzone 文档,我指定了:

  resizeWidth: 384,
  resizeHeight: 384,
  resizeMethod: 'contain',
  resizeQuality: 1.0,

Debugging the code, the resize functionality is called with the above settings.调试代码,使用上述设置调用调整大小功能。 However, the file that arrives at sending is still the original file which has not been resized.但是,到达发送的file仍然是未调整大小的原始文件。 I could not make out where the resized image would be stored.我无法弄清楚调整大小的图像将存储在哪里。

I tried to change sending as follows:我试图改变sending如下:

sending (file, xhr) {
  var _send = xhr.send;

  this.options.transformFile.call(this, file, function (done) {
    console.log('done', done)
    xhr.send = function () {
      _send.call(xhr, done);
    }
  }

However, the result from the call to transformFile is a blob and, while the result looks resized, it is also wrapped as a form.但是,调用transformFile的结果是一个 blob,虽然结果看起来已调整大小,但它也被包装为一个表单。

In summary, can I get the combination of resize with plain image upload to work, somehow?总之,我可以以某种方式将调整大小与普通图像上传相结合吗? Is the resized image stored in a suitable place?调整后的图像是否存储在合适的位置? Can my override of sending be changed to get this to function?可以更改我的sending覆盖以使其正常工作吗?

I just created an account to answer this since I've been trying to do the same thing for the last hours.我刚刚创建了一个帐户来回答这个问题,因为过去几个小时我一直在尝试做同样的事情。

I was on the same track as what you did, except I removed the resizeWidth and resizeHeight params to prevent double processing, and called resizeImage instead.我和你所做的在同一条轨道上,除了我删除了resizeWidthresizeHeight参数以防止双重处理,并改为调用resizeImage

After messing with logs for a while it hit me, it was a race condition!在弄乱日志一段时间后它击中了我,这是一个竞争条件! The function sending returned before xhr got modified.xhr被修改之前返回的sending函数。 Specifically, in createThumbnail there is a FileReader that executes async.具体来说,在createThumbnail有一个执行异步的FileReader Your result was resized and wrapped because the xhr did not get changed at all, so it just executed the normal code path.您的结果被调整大小和包装,因为xhr根本没有改变,所以它只是执行正常的代码路径。

Thus, just calling the resizing function in xhr.send does the trick!因此,只需在xhr.send中调用调整大小函数xhr.send 🎉🥳 🎉🥳

Here is my working code:这是我的工作代码:

sending: function(file, xhr) {
    var _send = xhr.send;
    var _this = this
    xhr.send = function () {
        _this.resizeImage(file, 500, 500, 'contain', function (done) {
            _send.call(xhr, done);
        })
    }
}

I asked the same on github and pkl's solution ( https://github.com/enyo/dropzone/issues/1840#issuecomment-538643878 ) worked for me:我在 github 和 pkl 的解决方案( https://github.com/enyo/dropzone/issues/1840#issuecomment-538643878 )上问了同样的问题,对我有用:

xhr.send = formData => {
  // get the resized file in formData.get("file") vs file
  _send.call(xhr, formData.get("file"));
 };

also, joshmaines posted ( https://github.com/enyo/dropzone/issues/1840#issuecomment-545590304 ):另外,joshmaines 发布( https://github.com/enyo/dropzone/issues/1840#issuecomment-545590304 ):

You could also override the process queue and implementing it in the thumbnail function instead: this.on('thumbnail', function(file, thumb) { file.thumbnail = thumb; this.processQueue(); });您还可以覆盖进程队列并在缩略图函数中实现它: this.on('thumbnail', function(file, thumb) { file.thumbnail = thumb; this.processQueue(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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