Home > Net >  How to copy or grab a file from stream and copy it to a folder in the server
How to copy or grab a file from stream and copy it to a folder in the server

Time:03-16

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;
        }

CodePudding user response:

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

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

  • Related