c# - Microsoft.Office.Interop 用于网站(文件转换)是否安全?

标签 c# asp.net security ms-word ms-office

我正在编写一个网站,用户必须添加报告(Word 文档)并可以查看它们,我将 *.doc 转换为 *.pdf,然后通过 pdf.js 显示它们。为了进行转换,我使用 Microsoft.Office.Interop.Word。代码看起来像

public void ConvertDocument(string PATH)
    {
        FileInfo FILE = new FileInfo(PATH);

        if (FILE.Extension.ToLower() == ".doc" || FILE.Extension.ToLower() == ".docx" || FILE.Extension.ToLower() == ".docm" || FILE.Extension.ToLower() == ".dotx" || FILE.Extension.ToLower() == ".dotm")
        {
            if (FILE.Length == 0)
            {
                return;
            }

            object oMissing = System.Reflection.Missing.Value;
            Word.Application word = new Word.Application();

            try
            {
                word.Visible = false;
                word.ScreenUpdating = false;

                Object filename = (Object)FILE.FullName;
                Word.Document doc = word.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                try
                {
                    doc.Activate();
                    object outputFileName = FILE.FullName.Replace(FILE.Extension, ".PDF");
                    doc.SaveAs(ref outputFileName, Word.WdSaveFormat.wdFormatPDF, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                }

                finally
                {
                    object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                    ((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                    doc = null;
                }
            }

            finally
            {
                ((Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                word = null;
            }

            File.Delete(PATH);
        }
}
  1. 这样安全吗?
  2. 它将处理多少用户?
  3. 需要什么资源?
  4. 我应该在服务器上安装 MS Office 才能运行该网站吗?
  5. 这实际上是一个好方法吗?

最佳答案

微软的答案是否定的:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

来自Considerations for Server-Side Automation of Office

根据经验,以下是我们遇到的问题:

  • 当进程意外存在时,大量 Word 实例会残留下来,并且永远不会被清理
  • 处理 session 中途关闭后,Word 安装已损坏
  • 它的扩展性不是很好 - Word 是一个大型桌面应用程序,但它的功能非常出色;但是,它并不真正适用于您正在使用它的进程,因此,打开它的大量实例将消耗您的应用程序可以使用的资源。

但是,还有其他方法可以做到这一点,如 this StackOverflow question and answers 中所述。

您可以考虑预先转换word文档 - 例如,是否可以在上传文档时同时创建PDF?这样,您的服务器只需提供 PDF 文档,并且在处理请求时只需做很少的工作。

关于c# - Microsoft.Office.Interop 用于网站(文件转换)是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16780179/

相关文章:

c# - 从非泛型静态类中的泛型重载方法获取 RuntimeMethodInfo

java - 如何在 api url 中传递用户名和密码

c# - 这是轮询数据库的正确方法吗?

c# - Azure 移动服务 Web Api 上的 SignalR CORS

c# - 如何防止用户从多个位置或同一台电脑上的不同浏览器登录

javascript - ASP.NET session 无法自行更新

php - 控制试用订阅的访问

security - SSL 真的值得吗?

C# 按索引偶数升序奇数降序对数组重新排序

c# - 如何将字符串中单词之间的每个数字作为单独的值进行替换