如何使用 .NET 从 URL 中读取 docx 文件

How to read docx file from a URL using .NET

我想在.NET core 2.2 framework中使用web HTTP请求读取word文件的内容

我尝试了以下代码:

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

输出:

无法从 URL 读取 .docx 文件的内容。如何在没有任何付费图书馆或使用 HTTP 网络请求的情况下读取 docx 文件。

您可以使用 OpenXml 处理 word 文档: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;
    }
}