2014-09-24 49 views
-1

我开发了MVC4应用程序并托管在IIS服务器上。 在应用程序中,我创建了用于PPT和Word下载的模板。 它在本地完美工作,但在IIS服务器托管后,我无法下载它。 请向我提供解决方案&,这需要在服务器的组件服务或其他东西上进行设置。 我的代码是: 控制器:不从IIS服务器下载PPT/Word文件

public ActionResult DownloadFile(GlobalDashboardModel objGlobalDashboard) 
{ 
    //string contentType = "application/doc"; 
    objBSS = new BSS_Repository(); 

    var currentDate = Convert.ToDateTime(DateTime.Now).ToString("ddMMyyyyhhmmss"); 
    object temporaryFileName = Server.MapPath(_tempPathOnServer) + "GloablDashboard" + "-" + currentDate + ".doc"; 

    string tempPathOnClient = _tempPathOnClient + "GloablDashboard" + "-" + currentDate + ".doc"; 

    MSWord.Document document = null; 
    try 
    { 
      // processing for word document 
      object missing = Missing.Value; 

      MSWord.Application wordApp = new MSWord.Application(); 
      string templateFile = Server.MapPath(_globalDashboardTemplateFilePath); 

      // copy from template file to temporary file 
      System.IO.File.Copy(templateFile, (string)temporaryFileName, true); 

      if (System.IO.File.Exists(temporaryFileName.ToString())) 
      { 
       object readOnly = false; 

        object isVisible = false; 

        document = wordApp.Documents.Open(ref temporaryFileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); 

        document.Activate(); 

        //// Call FindAndReplace()function for each change 
        this.FindAndReplace(document, "<bsc>", objGlobalDashboard.bscCount); 
        this.FindAndReplace(document, "<bts>", objGlobalDashboard.btsCount); 
        this.FindAndReplace(document, "<nw>", objGlobalDashboard.networkAvailability); 

        //// save temp.doc after modified 
        document.Save(); 
       } 
       return Json(new { path = tempPathOnClient }, JsonRequestBehavior.AllowGet); 
       } 
       catch 
       { 
        ModelState.AddModelError(string.Empty, "File does not exist !!!"); 
       } 
       finally 
       { 
        if (document != null) 
        { 
         ((Microsoft.Office.Interop.Word._Document)document).Close(true, Type.Missing, Type.Missing); 

        } 
       } 
      return Json(new { path = tempPathOnClient }, JsonRequestBehavior.AllowGet); 
     } 
+0

你的代码看起来如何?什么是实际的错误?你有什么尝试? – bstenzel 2014-09-24 09:08:17

+0

我在我的应用程序中有一个表单,我希望它在word文件中,因为我已经创建了一个模板,并使用interop.word库在模板中写入值,并通过loation.href.Now在iis服务器上托管应用程序进行下载。此功能不起作用。 – coder 2014-09-24 09:16:57

+0

你需要发布一些代码! 'location.href'的价值是什么? – 2014-09-24 09:23:33

回答

0

我会做一个大胆的假设:问题不在于你无法下载文件。问题在于你的单词自动化不能在服务器上运行。我已经完成了服务器端的单词自动化,你的实现是 - 轻描淡写 - 乐观。

在本地工作站上运行应用程序时,它将以您的用户权限运行。在服务器上,它运行的权限非常有限。例如,Web用户帐户没有DCOM权限。我给了一个简短的概要在这里配置明智的做法:editing word document in c# asp.net

因为它是现在,您的实现可能会非常多地运行到服务器上的NullReferenceException。如果它没有首先遇到与互操作相关的异常。它很可能也会遭受内存泄漏。我强烈建议你在做任何事情之前在你的应用程序中实现一些日志记录。这样你就会知道实际的问题是什么。

此外,你不应该使用服务器端的字自动化,除非你绝对必须。您可能需要检查OpenXML SDK是否也满足您的要求。如果必须使用它,则必须在连接内存分析器的服务器上运行负载测试,以发现内存泄漏。