我想要检查是否有任何方法,我可以下载的IPFS文件,而不运行本地恶魔。我正在工作的Dapp,我将上传文件通过IPFS,并发送它的链接到各自的团队。我要那个团队下载文件。
发布于 2018-08-29 09:49:05
您可以在DApp中添加npm库“ipfs”。
https://www.npmjs.com/package/ipfs
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);
});
};// 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,该文件将被下载。
发布于 2018-11-24 07:59:51
尝试http网关网关- IPFS新闻
https://ethereum.stackexchange.com/questions/57659
复制相似问题