Mar*_*gan 6 .net c# abcpdf .net-3.5
我正在检索.docx文件作为字节数组.然后我试图用所述字节数组作为数据参数调用Doc的read()函数,但是我收到一个无法识别的文件扩展名错误.
我用以下(c#)代码检索字节数组:
WebClient testWc = new WebClient();
testWc.Credentials = CredentialCache.DefaultCredentials;
byte[] data = testWc.DownloadData("http://localhost/Lists/incidents/Attachments/1/Testdocnospaces.docx");
Run Code Online (Sandbox Code Playgroud)
如果此时我将字节数组输出为.docx文件,我的程序将正确地允许我打开或保存文件.出于这个原因,我相信已正确检索字节数组.以下是输出.docx文件的示例:
Response.ClearHeaders();
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;Filename=test.docx");
Response.BinaryWrite(data);
Response.Flush();
Response.End();
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将字节数组读入Doc,如下所示:
Doc doc = new Doc();
XReadOptions xr = new XReadOptions();
xr.ReadModule = ReadModuleType.MSOffice;
doc.Read(data, xr);
Run Code Online (Sandbox Code Playgroud)
我的程序将在所述代码的最后一行出错,抛出以下内容:"FileExtension''对于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);
Run Code Online (Sandbox Code Playgroud)
这种方法对我有用.当您提供正确的文件扩展名时,您不需要设置XReadOptions对象的ReadModule属性.ToUpper()不是强制性的.