我想在word文档中添加一个文件
application.Selection.InsertFile(file);
但它会导致异常
COMException: The document name or path is not valid
然后保存
document.SaveAs(path);
但是我得到了异常
The object invoked has disconnected from its clients. (Exception HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
完全相同的代码在从 Visual Studio 运行时有效。 异常 仅在我尝试从 IIS 运行时发生。
会不会跟某些权限有关?我已将完全控制权限授予 Everyone
、ISUSR
、Network
、Network Service
、System
, Administrators
...我还尝试使用具有管理员帐户的 Impersonate。
我如何修复它以便它在我从 IIS 运行时正常工作?
这是我为操作文档而创建的类:http://pastebin.com/yB2s0jn4
我是这样调用它的
using (var doc = new HtmlWordDocument(outFile))
{
// calls Selection.InsertFile( file )
doc.WriteContent(tempFile);
// calls document.SaveAs()
doc.Save();
}
最佳答案
您是否尝试过模拟 (How to implement impersonation in an ASP.NET application)?如下所示:
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
using (var doc = new HtmlWordDocument(outFile))
{
// calls Selection.InsertFile( file )
doc.WriteContent(tempFile);
// calls document.SaveAs()
doc.Save();
}
impersonationContext.Undo();
关于c# - 使用 `InsertFile` 并在 IIS 上保存来自网站的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8260885/