如何取消Formidable(Node.js)中的用户上传? [英] How to cancel user upload in Formidable (Node.js)?

查看:184
本文介绍了如何取消Formidable(Node.js)中的用户上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上工作了两天了,但我被困住了.我正在将Node.js与Express结合使用,并且正在尝试实现一个上传表单.基本上,我希望表单执行以下操作:

  • 检查文件的大小,如果文件太大则取消上传(当我说取消"时,我的意思是防止将更多数据写入磁盘并删除临时文件)

  • 检查文件类型,并确认它是正确的类型(.jpg,.png等),如果不是,请停止进一步写入磁盘并删除临时文件.

当前,我可以进行上传,当文件太大或与正确的类型不匹配时,我会发出错误消息,然后在将整个文件写入磁盘后,用fs.unlink()删除文件.但是我看到了这种方法的潜在问题:如果用户上传了一个巨大的文件(GB大小)怎么办?现在使用我的方法,最终将其从我的计算机中删除,但不会浪费大量资源.因此,基本上,我希望使用最少的资源,以确认文件可以上传.这是我到目前为止的代码:

    var path = absolutePath + '/public/images/users/' + req.session.userId + '/';
    var maxSize = 3146000; // 3MB
    var form = new formidable.IncomingForm();
    form.uploadDir = path;
    form.keepExtensions = true;

    form.on('error', function(message) {
        if(message)
        {
            res.json({err: message});
        }
        else
        {
            res.json({err: 'Upload error, please try again'});
        }
    });

    form.on('fileBegin', function(name, file){
        if(form.bytesExpected > maxSize)
        {
            this.emit('error', 'Size must not be over 3MB');
        }
    });

    form.on('file', function(name, file) {
        var type = file.type;
        type = type.split('/');
        type = type[1];

        if(type != 'jpeg' && type != 'png' && type != 'gif')
        {
            this.emit('error', "JPG's, PNG's, GIF's only");
            fs.unlink(file.path);
        }
        else
        {
            fs.rename(file.path, path + 'profile.' + type);
        }
    });

    form.on('progress', function(bytesReceived, bytesExpected) {
            console.log(bytesReceived); //This is just to view progress
    });

    form.parse(req);

我也很困惑,因为根据 https://github.com/felixge/node-formidable 上的文档,它说:

遇到错误的请求会自动暂停,如果您希望请求继续触发数据"事件,则必须手动调用request.resume().

这太好了,但是我似乎无法使它正常工作.每当我发出错误"消息时,数据"事件就会一直触发直到完成.

尝试

我尝试在发生错误时取消请求,但无济于事. req.pause()对我无济于事,req.end()req.abort()给我一个错误,指出它不是方法,而req.connection.destroy()req.connection.end()只是发送了POST请求循环.

最终想法

所以我正在寻找的东西似乎应该很平常,但是最近两天我一直在互联网上进行彻底的搜索,而我似乎找不到任何东西.我的意思是,在上传完整个文件之后,很容易检查文件的大小和类型,但是谁愿意浪费所有这些资源?更不用说恶意用户可以做的事情了.

我将继续努力,直到获得所需的确切信息为止,但我认为此问题可能与其他用户有关,希望我能获得一些帮助!

感谢您的时间.

解决方案

一些时间过去了,现在您可以使用

I am also confused because according to the documents at https://github.com/felixge/node-formidable, it says:

A request that experiences an error is automatically paused, you will have to manually call request.resume() if you want the request to continue firing 'data' events.

This would be great, but I can't seem to get it to work. Whenever I emit an 'error', the 'data' events keep firing until completion.

Attempts

I have tried canceling the request when an error is occurred, but to no avail. req.pause() did nothing for me, req.end() and req.abort() gave me an error saying that it wasn't a method, and req.connection.destroy() and req.connection.end() just sent a loop of POST requests.

Final Thoughts

So what I am looking for seems like it should be common-place, but I have spent the last two days searching the internet on a thorough implementation, and I can't seem to find anything. I mean, it is simple to check the size and type of the file AFTER the entire thing has been uploaded, but who wants to waste all of those resources? Not to mention the things that malicious users could do.

I will continue working until I get exactly what I am looking for but I thought that this issue may be relevant for some other users, and hopefully I can get some help!

Thanks for your time.

解决方案

Some time has passed and now you can use multer instead of formidable or multiparty. Multer has desired functionality built in.

这篇关于如何取消Formidable(Node.js)中的用户上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆