Check docx is corrupted or not

前端 未结 2 684
梦谈多话
梦谈多话 2021-01-27 04:15

I tried many solution but code is always checking corrupted file and send true

using (FileStream fileStream = File.OpenRead(path[0]))
{
    MemoryStream memStrea         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-27 04:49

    Take a look in this page: How to: Validate a word processing document.

    Using the Open XML SDK, you can write a code like this:

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

    But you should also check if the file was really damaged, or your download code isn't ok.

提交回复
热议问题