Getting error while converting pdf file to array from url

143 views Asked by At

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);
1

There are 1 answers

0
Chen On

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