'How to copy or grab a file from stream and copy it to a folder in the server

I am using syncfusion OCR to scan PDFs which produces a document and push it for download as the end result. I am trying to grab the file from the stream and put copy it to my server but i am getting an error saying stream does not support reading. Here is my code

try
        {

            string binaries = Path.Combine(this._hostingEnvironment.ContentRootPath, "Tesseractbinaries", "Windows");

            //Initialize OCR processor with tesseract binaries.
            OCRProcessor processor = new OCRProcessor(binaries);
            //Set language to the OCR processor.
            processor.Settings.Language = Languages.English;

            string path = Path.Combine(this._hostingEnvironment.ContentRootPath, @"Data\font", "times.ttf");
            FileStream fontStream = new FileStream(path, FileMode.Open);

            //Create a true type font to support unicode characters in PDF.
            processor.UnicodeFont = new PdfTrueTypeFont(fontStream, 8);

            //Set temporary folder to save intermediate files.
            processor.Settings.TempFolder = Path.Combine(this._hostingEnvironment.ContentRootPath, "Data");

            //Load a PDF document.
            FileStream inputDocument = new FileStream(Path.Combine(this._hostingEnvironment.ContentRootPath, "Data", "pistone.pdf"), FileMode.Open);
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);


            //Perform OCR with language data.
            string tessdataPath = Path.Combine(this._hostingEnvironment.ContentRootPath, "tessdata");
            //string tessdataPath = Path.Combine(@"tessdata");
            processor.PerformOCR(loadedDocument, tessdataPath);

            //Save the PDF document.
            MemoryStream outputDocument = new MemoryStream();
            loadedDocument.Save(outputDocument);
            outputDocument.Position = 0;

            //Dispose OCR processor and PDF document.
            processor.Dispose();
            loadedDocument.Close(true);

            //Download the PDF document in the browser.
            FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, "application/pdf");
            fileStreamResult.FileDownloadName = "OCRed_PDF_document.pdf";

            //setting a path for saving it to my server and copying it to the folder downloads
            string filePath = Path.Combine("downloads", fileStreamResult.FileDownloadName);
            using (Stream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
            {
                fileStream.CopyTo(fileStream);
            }



            return fileStreamResult;
        }
        catch (Exception ex)
        {

            throw;
        }


Solution 1:[1]

fileStream.CopyTo(fileStream) seems to be attempting to copy a stream to itself.

Try replacing with fileStreamResult.FileStream.CopyTo(fileStream) ?

Solution 2:[2]

We can resolve this error by using fileStreamResult.FileStream.CopyTo(fileStream) instead of fileStream.CopyTo(fileStream) in sample level. Please try the below code snippet or sample on your end and let us know the result.

Please find the below modified code snippet,

using (Stream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)){ fileStreamResult.FileStream.CopyTo(fileStream);}

Please refer to the below link for more information,

UG: https://help.syncfusion.com/file-formats/pdf/working-with-ocr/dot-net-core

KB: https://www.syncfusion.com/kb/11696/how-to-perform-ocr-in-asp-net-core-platform

Note: I work for Syncfusion.

Regards,

Gowtham K

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 timdar
Solution 2 gowtham raj