如何使用Node.js在Hyperledger中上载和检索文件(pdf,txt,..)

时间:2018-08-17 12:22:10

标签: node.js hyperledger-fabric

下面是我的代码上载和检索超分类帐中的图像

`const express = require('express');
const multer = require('multer');
var fs = require("fs");
const upload = multer({
  dest: 'uploads/' // this saves your file into a directory called "uploads"
}); 

const app = express();

// function to encode file data to base64 encoded string
function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    var Img_encode = new Buffer(bitmap).toString('base64');
    fs.unlink(file);
    return Img_encode;
}

// function to create file from base64 encoded string
function base64_decode(base64str, file) {
    // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
    var bitmap = new Buffer(base64str, 'base64');
    // write buffer to file
    fs.writeFileSync(file, bitmap);
    console.log('******** File created from base64 encoded string ********');
}

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

// It's very crucial that the file name matches the name attribute in your html
app.post('/', upload.array('file-to-upload',2), (req, res) => {

    var base64str = base64_encode(req.files[0].path);
    var decode_img = base64_decode(base64str, 'copy.jpg');
    res.send(base64str);
});

app.listen(3000,(req,res)=>{
    console.log("Listen @ 3000");
});`

我将base64str值存储到hyperleder,它可以很好地用于图像上载和检索,如何使用node将pdf或txt或doc之类的文本文件上载到hyperledger?任何帮助表示赞赏!!

0 个答案:

没有答案