【问题标题】:How to copy or grab a file from stream and copy it to a folder in the server如何从流中复制或抓取文件并将其复制到服务器中的文件夹
【发布时间】:2022-10-02 04:32:09
【问题描述】:

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

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

    标签: c# asp.net-mvc syncfusion


    【解决方案1】:

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

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

    【讨论】:

    • 是的,它正在复制到流中,因此出现了错误。添加 fileStreamResult 修复它
    【解决方案2】:

    我们可以通过使用解决此错误fileStreamResult.FileStream.CopyTo(fileStream)代替文件流。复制到(文件流)在样本层面。请尝试以下代码 sn-p 或您的示例,并让我们知道结果。

    请找到以下修改后的代码 sn-p,

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

    请参阅以下链接以获取更多信息,

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2021-08-17
      相关资源
      最近更新 更多