如何使用的SuperAgent发送文件 [英] How to send files with superagent

查看:3379
本文介绍了如何使用的SuperAgent发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,大约一个月前,我问<一个href="http://stackoverflow.com/questions/30738201/sending-files-over-ajax-superagent-to-a-php-backend-laravel">a问题有关的SuperAgent和发送文件,但没有人回应的。我仍想了解如何为我喜欢使用SuperAgent的做到这一点。

So about a month ago I asked a question regarding superagent and sending files, but got no response at all. I would still like to find out how to do this as I enjoy using superagent.

我能够发送使用纯AJAX文件:

I am able to send files using plain ajax:

var fd = new FormData();
        fd.append( 'file', this.refs.File.getDOMNode().files[0] );

        $.ajax({
            url: 'http://localhost:8080/files',
            data: fd,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function(data){
                console.log(data)
            }
        });

但是当我尝试在SuperAgent的同样的事情,没有什么作品:

But when I try the same thing in superagent, nothing works:

var fd = new FormData();
fd.append( 'file', this.refs.File.getDOMNode().files[0] );

Request.post('http://localhost:8080/files')
    .set('Content-Type', false)
    .set('Process-Data', false)
    .attach('file', fd, 'file')
    .end((err, res) => {
        console.log(err);
        console.log(res);
    })

任何人都可以,请告诉我怎么回事。

Can anyone, please, tell me whats going on.

推荐答案

这应该工作。

var file = this.refs.File.getDOMNode().files[0];


Request.post('http://localhost:8080/files')
    .set("Content-Type", "application/octet-stream")
    .send(file)
    .end((err, res) => {
        console.log(err);
        console.log(res);
    })

这篇关于如何使用的SuperAgent发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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