1

I need to upload multiple files into ipfs from node js.

Flow:
file(html,angularJS) --> node.js --> IPFS --> ipfs hash save into DB.

I have sent a file from HTML(angularJS) and I got the file in node.js by the help of the multer npm module. With this solutions, the file is uploaded into a node.js server and then from there it is uploaded into IPFS.

Since I'm using azure for my node.js server, I cannot upload files. Is there an alternative approach to upload files into IPFS without uploading them to a node.js server?

html:

<form role="form" ng-submit="uploadComplete(form)" enctype="multipart/form-data" name="form.test">
    <input type="file" id="docName" name="docName" multiple class="form-control" 
           accept=".jpg,.png,.jpeg,.doc,.docx,.pdf" file-model="myFiles($files)"
           placeholder="Enter the docName here" required aria-required="true">
</form>

AngularJS:

var formData = new FormData();
$scope.myFiles = function ($files) {
    formData.append('img', $files[0]);
};

// Method for sign up to the kyc system
$scope.uploadComplete = function (form) {
    formData.append('email', $scope.email);
    formData.append('password', $scope.password);                 
    var promise = uploadFileToUrl(formData, uploadUrl);            
}

var uploadFileToUrl = function (formData, uploadUrl) {
    var deffered = $q.defer();
    $http.post(uploadUrl, formData, {
        transformRequest: angular.identity,
        headers: { 'Content-Type': undefined }
    }).success(function (response) {
        deffered.resolve(response);
    }).error(function (response) {
        deffered.reject(response);
    });
    return deffered.promise;
};

Node.js:

var storage = multer.diskStorage({
    destination: './uploads/',
    filename: function (req, file, cb) {
        stData = file.stream;
        cb(null, file.originalname.replace(path.extname(file.originalname), "") + '-' + Date.now() + path.extname(file.originalname))
    },   
    stream: function (req, file, cb) {
        cb(null, file.stream);
    }
});

var upload = multer({ storage: storage });

app.post('/api/user/new', upload.single('img'), (req, res) => {
    if (!req.file) {
        return res.status(422).json({
            error: 'File needs to be provided.'
        });
    }

    createNewUser(req,res,stData);
}

var createNewUser = function (req, resp,stData) {       
    var filesStreamm = [{
        path: req.file.originalname,
        // content: fs.readFileSync(req.file.path)
        content: ipfs.types.Buffer.from(stData)
        // content: fs.createReadStream(stData) //stData
    }];

    var deferred = Q.defer();
    deferred.notify();
    ipfs.add(filesStreamm, function (err, files) {
        if (err || !files) {
            deferred.reject();
            return console.error('ipfs add error', err);
        }
        return resp.json({ status: 'SUCCESS', message: 'Published Succesfully' });
    }

closed as off-topic by smarx, Achala Dissanayake, shane, Ismael, ivicaa Jan 13 at 18:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – smarx, Achala Dissanayake, shane, Ismael, ivicaa
If this question can be reworded to fit the rules in the help center, please edit the question.

0

Yeah, you can do. I guess you're using Express.js to handle a request and response If you want to upload a file to the server you need to handle a multipart request. Read the file from the request and use the IPFS node module to upload file content to IPFS.

Currently, I am using http module

Please find below sample code for the same:

// Node server
var http = require('http');
const ipfsAPI = require('ipfs-api');
var formidable = require('formidable');
var fs = require('fs');
http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var uploadedFile = fs.readFileSync(oldpath);
      var picBuffer = new Buffer(uploadedFile);
      ipfs.files.add(picBuffer, function (err, file) {
          if (err) {
             console.log(err);
          }
          console.log(file)
      })
      //TODO Use node ipfs module to save file into network,
    })
});

If you want to save file into IPFS network, you can look into Infra

Not the answer you're looking for? Browse other questions tagged or ask your own question.