无法读取IIS服务器上的文档 [英] unable to read document on IIS Server

查看:107
本文介绍了无法读取IIS服务器上的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在开发服务器上运行完美,如果在IIS服务器上运行会出错,帮助



  if (arrext [ 1 ]。ToString()==   doc || arrext [ 1 ]。ToString()==   docx
{
Document doc = new Document();

fileContent = WordCopy(dt.Dep_FileName);
}





  public   string  WordCopy( string  filePath)
{
Application word = new Application();
object miss = System.Reflection.Missing.Value;
object file = @ D:\\ \\ New System \SYSTEMPRO \FileUpload \ + filePath;
object readOnly = true ;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open( ref file, ref miss, ref readOnly, ref miss, ref 小姐,参考小姐,参考小姐, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = ;
for int i = 0 ; i < docs.Paragraphs.Count; i ++)
{
totaltext + = \\\\ n + docs.Paragraphs [i + 1 ]。 Range.Text.ToString();
}
return totaltext;
}

解决方案

您的代码是自动化Word,因此您需要在服务器上安装Word。 Word是一个桌面应用程序,它在多线程非GUI环境(如Web服务器)中无法正常可靠地工作。那是在我们甚至触及你想要做的事情的合法性之前。



如果你想从asp.net操纵Word文档,你需要使用为Aspose设计的东西,或 DocX [ ^ ]。


这显然是其他人声明的权限问题。阅读 [ ^ ],应该解决你的问题


看看这个D:\New System \SYSTEMPRO \FileUpload 。原则上它不能表现不明显。



即使在一般编程中,也没有硬编码文件路径有用的情况。在服务器端编程中,情况更糟。谁告诉你这个磁盘和目录存在于服务器部分?即使它存在,也无法奏效。 Web应用程序在沙盒环境中执行,无论其权限属性如何,都会阻止代码访问随机文件系统对象。只能访问根目录下设置的文件和目录。



所以,你必须只使用相对路径或相对于路径的路径网站的根。但是使用.NET FCL通常需要完整的路径名,可以使用 MapPath 计算:

HttpServerUtility.MapPath Method(String)(System.Web) [ ^ ],

服务器。 MapPath方法 [ ^ ]。







此外,这是一个坏主意在服务器设置中使用Office互操作。你怎么知道它安装在服务器端?请参阅以下微软文章警告:

http ://support.microsoft.com/default.aspx?scid = kb; EN-US; q257757 #kb2 [ ^ ],

http://support.microsoft.com/kb/257757/en-us [ ^ ]。



我建议使用Microsoft Open XML格式SDK。请看我过去的答案:

从gridview或asp.net中的数据表记录创建doc文件c# [ ^ ],

在C#.net中,如何从目录中打开.docx文件并将其显示在浏览器? [ ^ ]。



-SA

Hi, my code runs perfectly on development server while it gives error if run on IIS server, help

if (arrext[1].ToString() == "doc" || arrext[1].ToString() == "docx")
{
    Document doc = new Document();
    
    fileContent = WordCopy(dt.Dep_FileName);
}



public string WordCopy(string filePath)
{
    Application word = new Application();
    object miss = System.Reflection.Missing.Value;
    object file = @"D:\New System\SYSTEMPRO\FileUpload\" + filePath;
    object readOnly = true;
    Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref file, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
    string totaltext = "";
    for (int i = 0; i < docs.Paragraphs.Count; i++)
    {
        totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
    }
    return totaltext;
}

解决方案

Your code is automating Word, so you need to have Word installed on the server. Word is a desktop app, it won't work properly and reliably in a multi-threaded non-GUI environment like a web server. That's before we've even touched on the legality of what you're trying to do.

If you want to manipulate Word docs from asp.net you'll need to use something designed for the purpose like Aspose, or DocX[^].


This is clearly a permission issue as stated by others. Read this[^], should solve your issue


Just look at this "D:\New System\SYSTEMPRO\FileUpload". Isn't it obvious that it cannot work in principle.

Even in general programming, there are no situations when hard-coded file paths can be useful. In server-side programming, it's even worse. Who told you that this disk and directory exist on the server part? And even if it exists, it cannot work. A Web application is executed in sandboxed environment which prevents the code from access to a random file system object, regardless of its permission attributes. Only the access to files and directories under the root directory set up to the side is possible.

So, you have to use only relative paths, or paths relative to the site's root. But the use of .NET FCL usually requires full path names, which can be calculated using MapPath:
HttpServerUtility.MapPath Method (String) (System.Web)[^],
Server.MapPath Method[^].

[EDIT]

Also, it's a bad idea to use Office interop in the server settings. How do you know it's installed on the server side? Please see these Microsoft articles warning against that:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

I would recommend to use Microsoft Open XML Format SDK. Please see my past answers:
Create a doc file from gridview or datatable records in asp.net c#[^],
In C#.net, how do I open .docx files from a directory and display it in the browser?[^].

—SA


这篇关于无法读取IIS服务器上的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆