I am working on a website, which allows users to upload different file formats. We need to restrict the user from uploading password protected files.
Is there a way to determine if a Microsoft Office file (Word, Powerpoint & Excel) is password protected before uploading the file?
As per http://social.msdn.microsoft.com/Forums/en/oxmlsdk/thread/34701a34-f1d4-4802-9ce4-133f15039c69, I have implemented the following, but it throws an error saying "File contains corrupted data", while trying to open a password protected file.
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, false))
{
DocumentProtection dp =
wordDoc.MainDocumentPart.DocumentSettingsPart.Settings.GetFirstChild<DocumentProtection>();
if (dp != null && dp.Enforcement == DocumentFormat.OpenXml.OnOffValue.FromBoolean(true))
{
return true;
}
}
Are there any other ways to determine this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…