Go-Ethereum

如何從節點 js 將文件上傳到 ipfs 而不將文件上傳到節點 js 伺服器

  • January 8, 2019

我需要從節點 js 將多個文件上傳到 ipfs。

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

我從 HTML(angularJS) 發送了一個文件,並在multernpm 模組的幫助下在 node.js 中獲得了該文件。使用此解決方案,文件被上傳到 node.js 伺服器,然後從那裡上傳到 IPFS。

由於我將 azure 用於我的 node.js 伺服器,因此我無法上傳文件。是否有另一種方法可以將文件上傳到 IPFS 而無需將它們上傳到 node.js 伺服器?

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;
};

節點.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' });
   }

是的,你可以做到。我猜您正在使用 Express.js 來處理請求和響應如果您想將文件上傳到伺服器,您需要處理多部分請求。從請求中讀取文件並使用 IPFS 節點模組將文件內容上傳到 IPFS。

目前,我正在使用 http 模組

請在下面找到相同的範常式式碼:

// 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,
   })
});

如果要將文件保存到 IPFS 網路中,可以查看 Infra

引用自:https://ethereum.stackexchange.com/questions/65186