将字符串作为文件上传

时间:2017-06-14 16:20:04

标签: node.js http file-upload upload unirest

如何使用var applyCtor = function(){ var tempCtor = function() {}; return function(ctor, args){ tempCtor.prototype = ctor.prototype; var instance = new tempCtor(); ctor.prototype.constructor.apply(instance,args); return instance; } }(); var CarClass = (function (){ function car(doorsClass){ this.doorsClass = doorsClass; } car.prototype.color = function(color){ console.log('Car color is '+color); } car.prototype.doorsNum = function(doorsNum){ console.log('Car has '+this.doorsClass.doors(doorsNum)+' doors'); } return car; })(); //<-- execute your module var DoorsClass = (function (){ function doors(){ } doors.prototype.doors = function(num){ return num; } return doors; })();//<-- execute your module // Apply the given ctor with arguments. var carInstance = applyCtor(CarClass,[new DoorsClass()]); carInstance.color('red'); carInstance.doorsNum(4);

将字符串作为文件上传,并发送http帖子请求

以下是我的尝试:

node.js

但没有任何反应

1 个答案:

答案 0 :(得分:0)

You must specify a file from the local file system. So I advise

  1. you create the file locally

    var fs = require('fs');
    fs.writeFile("/tmp/test", "some text in file", function(err) {
        if(err) { return console.log(err);  }
        console.log("The file was saved!");
    }); 
    
  2. send the file

    var unirest = require('unirest');
    unirest.post('127.0.0.1/upload')
        .headers({'Content-Type': 'multipart/form-data'})
        .attach('file', '/tmp/test') 
        .end(function (response) {
           console.log(response.body);
        });