Search code examples
c#.netconsole-applicationnovacode-docx

Unable to load file from URL using DocX.Load


This code is working nicely with local files, but getting error when i refer fileName from URL using DocX library of Novacode. Kindly provide some way to resolve this. Thank you.

        try
        {
            string fileName = "http://api.92logics.com/myfile.docx";
            DocX doc = DocX.Load(filePath);
            int TotalLists = doc.Lists.Count;
        }
        catch (Exception ex)
        {
            string ErrorMessage = ex.Message;
        }

Solution

  • Found Solution using Stream Parameter It was generating error with Url, However when i tried second parameter of DocX.Load then with some code round found it is working with Stream to use remote file.

    Here is Code for reference for anyone else looking this solution.

    Stream streamObject = GetStreamFromUrl(filePath);
    DocX doc = DocX.Load(streamObject);
    
    private static Stream GetStreamFromUrl(string url)
    {
        byte[] imageData = null;
        using (var wc = new WebClient())
        {
            imageData = wc.DownloadData(url);
        }
        return new MemoryStream(imageData);
    }