文件上传之服务器检测,如何检测如果Word文档密码的文件上传到服务器之前保护?(How to detect if a Wo...

以下是在.aspx源文件

Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="TestApp.TestForm"

!DOCTYPE html PUBLIC

Reference Page ="~/TestForm.aspx" // Note: Removed all HTML tags

protected void Upload_Click(object sender, EventArgs e)

{

String noPW = "C:\\Users\\David\\Desktop\\Doc1.docx";

String pwProtected = "C:\\Users\\David\\Desktop\\Test.docx";

// if (isProtected(pwProtected))

// outcome.Text = ("Document Is Password Protected");

// else

// outcome.Text = ("Document Is NOT Password Protected");

if (isProtected(noPW))

outcome.Text = ("Document Is Password Protected");

else

outcome.Text = ("Document Is NOT Password Protected");

}

以下是在.aspx.cs代码文件的背后

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.Office.Interop.Word;

using System.Runtime.InteropServices;

using Microsoft.Office.Interop.Word;

namespace TestApp

{

public partial class TestForm : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

public static bool isProtected(object filePath)

{

Application myapp = new Application();

object pw = "thispassword";

try

{

// Trying this for Word document

myapp.Documents.Open(ref filePath, PasswordDocument: ref pw); // try open

myapp.Documents[ref filePath].Close(); // close it if it does open

}

catch (COMException ex)

{

if (ex.HResult == -2146822880) // Can't Open Doc Caused By Invalid Password

return true;

else

Console.WriteLine(ex.Message + " " + ex.HResult); // For debugging, have only tested this one document.

}

return false;

}

}

}

至少在我的电脑上,我得到两个文件预期的输出,但是这不正是你所谓的代码的详尽测试。 此外,我试图上传使用FileUpload控件文件,我得到了COM错误“无法找到C:\ WINDOWS \ SYSTEM \ fileName.docx”,这让我感到困惑,只是因为上传的文件从我的桌面来了,但你大概知道为什么发生,你更熟悉ASP.NET比我。 无论哪种方式,这个代码只是一些尝试,希望帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值