gpt4 book ai didi

c# - 检查docx是否损坏

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:29 25 4
gpt4 key购买 nike

我尝试了很多解决方案,但代码总是检查损坏的文件并发送 true

using (FileStream fileStream = File.OpenRead(path[0]))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
HttpContext.Current.Response.BinaryWrite(memStream.ToArray());
HttpContext.Current.Response.Flush();
// HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}

路径 [0] 是我的 docx 位置..它仍然读取损坏的文件并且不会抛出任何错误..任何建议 plz..

最佳答案

查看此页面:How to: Validate a word processing document .

使用 Open XML SDK ,你可以写这样的代码:

public static void ValidateWordDocument(string filepath)
{
using (var wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
try
{
OpenXmlValidator validator = new OpenXmlValidator();
int count = 0;
foreach (ValidationErrorInfo error in
validator.Validate(wordprocessingDocument))
{
count++;
Console.WriteLine("Error " + count);
Console.WriteLine("Description: " + error.Description);
Console.WriteLine("ErrorType: " + error.ErrorType);
Console.WriteLine("Node: " + error.Node);
Console.WriteLine("Path: " + error.Path.XPath);
Console.WriteLine("Part: " + error.Part.Uri);
Console.WriteLine("-------------------------------------------");
}

Console.WriteLine("count={0}", count);
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

wordprocessingDocument.Close();
}
}

但是你也应该检查文件是否真的损坏了,或者你的下载代码不对。

关于c# - 检查docx是否损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com