首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPFS文件下载

IPFS文件下载
EN

Ethereum用户
提问于 2018-08-29 08:14:40
回答 2查看 2.6K关注 0票数 4

我想要检查是否有任何方法,我可以下载的IPFS文件,而不运行本地恶魔。我正在工作的Dapp,我将上传文件通过IPFS,并发送它的链接到各自的团队。我要那个团队下载文件。

EN

回答 2

Ethereum用户

发布于 2018-08-29 09:49:05

您可以在DApp中添加npm库“ipfs”。

https://www.npmjs.com/package/ipfs

lib/ipfs.js

代码语言:javascript
复制
const IPFS = require('ipfs');
const node = new IPFS();

 /**
 * Function that uploads file onto IPFS
 * @param {JSON} fileDetails Json containing file details that needs to be 
   updated on IPFS
 * @param {function} callback callback function
 */
const uploadFileToIPFS = function(fileDetails, callback) {
    let fileContent = fs.readFileSync(fileDetails.path);
    node.files.add({
        path: fileDetails.originalname,
        content: fileContent
    }, (err, filesAdded) => {
        if (err) {
            return callback(err);
        }
        callback(null, {
            hash: filesAdded[0].hash
        });
    });
};


/**
 * Function that gets the document contents from IPFS, based on the document's hash
 * @param {String} ipfsHash IPFS hash of the document that is being uploaded onto IPFS
 * @param {function} callback callback function
 */
const getFileContentsFromIPFS = function(ipfsHash, callback) {
    node.files.cat(ipfsHash, function(err, data) {
        callback(err, data);
    });
};

routes/ipfs.js

代码语言:javascript
复制
// API that gets the documents from IPFS
router.get('/api/to/get/document/from/ipfs', function(req, res){
    ipfs.getFileContentsFromIPFS(req.query.ipfsHash, function(err, result){
        if (err) {
            res.json({
                status: 'error',
                message: 'Something went wrong while fetching document from IPFS'
            });
        } else {
            //Get the file type
            let isImageOrPdf = fileType(result);
            if (isImageOrPdf.ext == 'pdf') {
                res.writeHead(200,{
                    'Content-type': 'application/pdf'
                });
                res.end(result);
            } else if(isImageOrPdf.ext == 'png' || isImageOrPdf.ext == 'jpg' ){
                res.writeHead(200, {'Content-Type' : 'image/png'});
                res.end(result);
            }
        }
    });
});

而不是发送链接,而是发送文档的ipfsHash,每当他们想下载该文件时,获取ipfsHash并点击上面的API,该文件将被下载。

票数 2
EN

Ethereum用户

发布于 2018-11-24 07:59:51

尝试http网关网关- IPFS新闻

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/57659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档