0

I want to convert pdf file to array to further usage.. I am using Aspose PDF

I am getting PDF file from URL like this : "https:\testuploadsnewversion.blob.core.windows.net\files\740.pdf"

I am trying to convert PDF to array using following way : But I am getting this error

System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. : 'E:\Projects\https:\testuploadsnewversion.blob.core.windows.net\files\740.pdf'

below is code I am trying

byte[] buff = null;

            // Initialize FileStream object
            FileStream fs = new FileStream("https:\testuploadsnewversion.blob.core.windows.net\files\740.pdf", FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(file.FilePath).Length;

            // Load the file contents in the byte array
            buff = br.ReadBytes((int)numBytes);
            fs.Close();

            Stream pdfStream = new MemoryStream(buff);

            Document pdfDocument = new Document(pdfStream);
Sami In
  • 246
  • 2
  • 11
  • Use httpclient to download the file. – Magnus Jul 03 '23 at 21:05
  • It looks like the file is hosted on some URL. In this case, you need to first download the bytes of the file (PDF) and then initialize Document() instance with received bytes. In case issue still persists, please create a post in our official support forum i.e. https://forum.aspose.com/c/pdf and we will further proceed to assist you accordingly. This is Asad Ali and I am Developer Evangelist at Aspose. – Asad Ali Jul 10 '23 at 20:28

1 Answers1

0

Is the url you got a web address? Why is it https:\ instead of https://?

If it's just a typo on your part, then you can follow the code below.

Create a DownloadExtention.cs :

public class DownloadExtention
{
    public static async Task<byte[]?> GetUrlContent(string url)
    {
        using (var client = new HttpClient())
        using (var result = await client.GetAsync(url))
            return result.IsSuccessStatusCode ? await result.Content.ReadAsByteArrayAsync() : null;
    }
}

Then in the API method, create a method as below:

[HttpGet("downloadfromurl")]
public IActionResult DownloadFromUrl()
{
    string url = "https://www.learningcontainer.com/wp-content/uploads/2020/07/Large-Sample-Image-download-for-Testing.jpg";
    byte[] buff = null;
    var result = DownloadExtention.GetUrlContent(url);
    if (result != null)
    {
        buff = result.Result;
        //do something
        return File(result.Result, "image/png", "test.jpg");
    }
    return Ok("file is not exist");
}

Test Result:

enter image description here

Chen
  • 4,499
  • 1
  • 2
  • 9