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

问题描述 投票:-1回答:1

我想在.NET核心2.2框架中使用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);
}

输出:enter image description here

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

c# asp.net-core openxml-sdk
1个回答
3
投票

您可以使用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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.