我正在检索一个字节数组形式的 .docx 文件。然后,我尝试使用所述字节数组作为数据参数调用 Doc 的 read() 函数,但我收到无法识别的文件扩展名错误。
我使用以下 (c#) 代码检索字节数组:
WebClient testWc = new WebClient();
testWc.Credentials = CredentialCache.DefaultCredentials;
byte[] data = testWc.DownloadData("http://localhost/Lists/incidents/Attachments/1/Testdocnospaces.docx");
如果此时我将字节数组输出为 .docx 文件,我的程序将正确地允许我打开或保存该文件。出于这个原因,我相信字节数组已被正确检索。这是我所说的输出 .docx 文件的示例:
Response.ClearHeaders();
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;Filename=test.docx");
Response.BinaryWrite(data);
Response.Flush();
Response.End();
但是,如果我尝试像这样将字节数组读入 Doc:
Doc doc = new Doc();
XReadOptions xr = new XReadOptions();
xr.ReadModule = ReadModuleType.MSOffice;
doc.Read(data, xr);
我的程序将在上述代码的最后一行出错,抛出以下内容:“FileExtension '' was invalid for ReadModuleType.MSOffice。”
Doc.Read() 函数似乎在通常会查找文件类型的位置查找空字符串。 另外,我在这台机器上安装了 Office 2007。
最佳答案
如果您知道文件字节的文件扩展名(您应该知道),您可以通过以下方式解决问题:
Doc doc = new Doc();
string extension = Path.GetExtension("your file name/path").Substring(1).ToUpper();
XReadOptions opts = new XReadOptions();
opts.FileExtension = extension;
doc.Read(fileBytes, opts);
这种方法对我有用。当您提供正确的文件扩展名时,您不需要设置 XReadOptions 对象的 ReadModule 属性。 ToUpper() 不是强制性的。
关于c# - 无法使用 ABCPDF 将字节数组(从 .docx 文件创建)读入 Doc 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13842227/