美文网首页
在Node环境,Bmob RestFul上传成功返回JSON数据

在Node环境,Bmob RestFul上传成功返回JSON数据

作者: 黄秀杰 | 来源:发表于2019-12-22 00:13 被阅读0次

    尝试使用request.js与needle.js模块作上传,都告失败

    模块一,request.js

    // request 上传
        var request = require('request');
        // header
        var formData = {
            // Pass data via Streams
            file: fs.readFileSync(file_url),
        };
        request.post({
            headers: {
                "X-Bmob-Application-Id": "9a22a7cb3217e88cac2e81fe7e88c600",
                "X-Bmob-REST-API-Key": "025c1282712e387a62b2bcd5a970f93c"
            },
            url: 'https://api.bmob.cn/2/files/' + filename,
            formData: formData
        }, function optionalCallback(err, httpResponse, body) {
            if (err) {
                return console.error('upload failed:', err);
            }
            res.send(body);
            // console.log('Upload successful!  Server responded with:', body);
        });
    

    使用request时还尝试了

    multipart: [{
                'content-type': 'image/jpeg',
                body: fs.readFileSync(file_url)
            }]
    

    以代替上述formData: formData,也一样失败了

    模块二,needle.js

    // neddle 上传
        var needle = require('needle');
        var data = {
            image: { file: file_url, content_type: 'image/png' }
        }
        var options = {
            multipart: true,
            headers: {
                "X-Bmob-Application-Id": "4552ad36df85c1f29953ae3679c69248",
                "X-Bmob-REST-API-Key": "1d1136d0ff2c112fd3b86cb4368126f1"
            }
        }
        needle.post('https://api.bmob.cn/2/files/' + filename, data, options, function(err, resp, body) {
            if (err) {
                console.log(err);
            }
            console.log(body);
            res.send(body);
            // needle will read the file and include it in the form-data as binary
        });
    

    ftp查看结果

    image.png

    显示0字节

    Bmob后台查看结果

    image.png

    显示真实文件大小

    上传回调结果

    image.png

    也就是说与又拍云通信正常

    浏览器中显示图裂了

    image.png

    这是哪里出了差错,能帮忙看看不?谢谢。

    相关文章

      网友评论

          本文标题:在Node环境,Bmob RestFul上传成功返回JSON数据

          本文链接:https://www.haomeiwen.com/subject/wviomxtx.html