【问题标题】:How to download a pdf file from IPFS usng ipfs core api in .net?如何在 .net 中使用 ipfs core api 从 IPFS 下载 pdf 文件?
【发布时间】:2019-03-18 16:30:50
【问题描述】:

最近我正在尝试将文件上传到 IPFS 并使用 ipfs 核心 api 下载/检索它。为此,使用 .net 库 ipfs(c#) library。它适用于 txt 文件,但是当我上传 pdf 文件并尝试下载时,它给了我某种字符串。我认为该字符串可能是我的 pdf 文件的所有内容,但该字符串证明我错了。当我尝试将我的原始 pdf 文件字符串与完全不同的(当前字符串)进行比较时..

我的 pdf 文件哈希:QmWPCRv8jBfr9sDjKuB5sxpVzXhMycZzwqxifrZZdQ6K9o

和我的 c# 代码获取 this(api) ==>

        static void Main(string[] args)
    {
        var ipfs = new IpfsClient();

        const string filename = "QmWPCRv8jBfr9sDjKuB5sxpVzXhMycZzwqxifrZZdQ6K9o";
        var text = ipfs.FileSystem.ReadAllTextAsync(filename).Result;
    }

我的问题是我做错了什么,我做错了什么,那么我怎样才能得到一个 pdf 文件?怎么样??

【问题讨论】:

    标签: ipfs


    【解决方案1】:

    首先请检查您是否可以从实时环境访问该文件: 例如

    https://ipfs.infura.io/ipfs/QmNtg1uDy1A71udMa2ipTfKghArRQFspfFkncunamW29SA

    https://ipfs.io/ipfs/

    如果文件上传正确你可以通过IpfsClient包来做这个动作:

    1. 定义在 ipfs env 上引用的属性(例如通过 infura)

      _ipfsClient = new IpfsClient("https://ipfs.infura.io:5001");

    2. 介绍hash下载文件的方法

        public async Task<byte[]> DownloadAsync(string hash) 
        {
            using (var stream = await _ipfsClient.FileSystem.ReadFileAsync(hash))
            {
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    return ms.ToArray();
                }
            }
        }
    
    1. 如果您使用 web api - 引入控制器以准确返回 pdf
        public async Task<IActionResult> Get(string hash)
        {
            var data = await _ipfsDownloadService.DownloadAsync(hash);
            return File(data, "application/pdf");
        }
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 2018-12-04
      • 2021-06-30
      • 1970-01-01
      • 2022-06-11
      • 2017-01-04
      • 2022-07-03
      • 2016-03-09
      • 1970-01-01
      相关资源
      最近更新 更多