0

Recently i am trying to upload a file to IPFS and download/retrieve it using ipfs core api. And for this purpose a use .net library ipfs(c#) library. its works fine for a txt file but when i uploaded a pdf file and tries to download it gives me some kind of string.i thought that that string maybe my pdf file all content but that string proves me wrong. when i tries to compare my original pdf file string with (current string) that is totally diffferent..

my pdf file hash : QmWPCRv8jBfr9sDjKuB5sxpVzXhMycZzwqxifrZZdQ6K9o

and my c# code the get this(api) ==>

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

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

my question is whtat i have done wrong and i have done some wrong then how can i get a pdf file ?? how ??

Amjad
  • 330
  • 7
  • 22

1 Answers1

0

First of all please check if you can access to the file from live environment: e.g.

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

https://ipfs.io/ipfs/

if the file was uploaded correctly you can IpfsClient package to do this action:

  1. Define property that references on ipfs env (e.g. via infura)

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

  2. Introduce method to download the file by 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. If you use web api - introduce controller to return exactly pdf
    public async Task<IActionResult> Get(string hash)
    {
        var data = await _ipfsDownloadService.DownloadAsync(hash);
        return File(data, "application/pdf");
    }
Community
  • 1
  • 1
NILF
  • 367
  • 2
  • 7