Как прочитать файл DOCX из URL с помощью .NET - PullRequest
0 голосов
/ 28 февраля 2019

Я хочу прочитать содержимое файла слова, используя веб-HTTP-запрос в .NET core 2.2 framework.

Я попробовал следующий код:

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    // Download the Web resource and save it into a data buffer.
    byte[] myDataBuffer = myWebClient.DownloadData(body.SourceUrl);

    // Display the downloaded data.
    string download = Encoding.ASCII.GetString(myDataBuffer);
}

Вывод: enter image description here

Невозможно прочитать содержимое файла .docx с URL-адреса.Как я могу прочитать файл DOCX без какой-либо платной библиотеки или с помощью веб-запроса HTTP.

1 Ответ

0 голосов
/ 28 февраля 2019

Вы можете использовать OpenXml для обработки текстового документа: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc535598(v=office.14)

Это, вероятно, то, что вы ищете:

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    // Download the Web resource and save it into a data buffer.
    byte[] bytes = myWebClient.DownloadData(body.SourceUrl);
    MemoryStream memoryStream = new MemoryStream(bytes);

    // Open a WordprocessingDocument for read-only access based on a stream.
    using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(memoryStream, false))
    {
        MainDocumentPart mainPart = wordDocument.MainDocumentPart;
        content = mainPart.Document.Body.InnerText;
    }
}
...