如何从流中复制或抓取文件并将其复制到服务器中的文件夹

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

我正在使用 syncfusion OCR 扫描生成文档的 PDF,并将其推送下载作为最终结果。我试图从流中获取文件并将其复制到我的服务器,但我收到一条错误消息,提示 stream 不支持读取 。这是我的代码

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

fileStream.CopyTo(fileStream) 似乎正在尝试将流复制到自身。

尝试替换为 fileStreamResult.FileStream.CopyTo(fileStream) ?

我们可以使用 fileStreamResult.FileStream.CopyTo(fileStream) 而不是 fileStream.CopyTo(fileStream) 来解决这个错误在样本水平。请尝试下面的代码片段或示例,然后让我们知道结果。

请找到下面修改后的代码片段,

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

更多信息请参考下面link,

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

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

注意:我为 Syncfusion 工作。

此致,

高瑟姆 K