简体   繁体   English

无法下载Word文件

[英]Unable to download word files

We have hosted an asp.net (C#) application on the iis 8.5 server. 我们已经在iis 8.5服务器上托管了一个asp.net(C#)应用程序。

The application is used to download or preview word files and are working file for a while. 该应用程序用于下载或预览Word文件,并且暂时处于工作状态。 But then it stopped after the restart. 但随后它在重新启动后停止了。

normally when this happen we just close all instance of the word file and iis then it seems to start working. 通常,当发生这种情况时,我们只关闭word文件和iis的所有实例,然后它似乎开始工作。 But today it stopped even after restarting. 但是今天,即使重新启动它也停止了。

it is not hanging, but when i clicks on the link to preview or download, it shows waiting for Website url. 它没有挂起,但是当我单击链接进行预览或下载时,它显示正在等待网站URL。 but the page is still responsive. 但页面仍然响应。

When i check on the Task Manager of the server i can see a word process (Microsoft Word (32 bit) ) running, and when i close it it stops the website went the way it was before. 当我在服务器的任务管理器上检查时,我可以看到一个Word进程(Microsoft Word(32位))正在运行,当我关闭它时,它停止了网站的运行。

Event viewer does not show much on this regards . 事件查看器在这方面没有显示太多。 i used edge and chrome with no luck. 我用边缘和镀铬没有运气。

Here us the code snippet for previewing the document 这是我们预览文档的代码片段

if (result != "FALSE")
{
    lblMsg.Text = "CV Created for " + EMPNO;
    System.IO.FileInfo file = new System.IO.FileInfo(result);
    if (file.Exists)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);
        HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
        HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
        HttpContext.Current.Response.WriteFile(file.FullName);
        HttpContext.Current.Response.End();
    }
}

Code snipet for downloading the document 

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
//System.Web.HttpContext c = System.Web.HttpContext.Current;
string archiveName = String.Format("EmployeeCV-{0}.zip",
DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=" + archiveName);
using (ZipFile zip = new ZipFile())
{
    zip.AddFiles(filesToInclude, "CV");
    zip.Save(HttpContext.Current.Response.OutputStream);
}

HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
Directory.Delete(saveLocation, true);

Use MIME type to download files 使用MIME类型下载文件

    public class FileAPIController : ApiController
    {
        [HttpGet]
        [Route("api/FileAPI/GetFile")]
        public HttpResponseMessage GetFile(string fileName)
        {
            //Create HTTP Response.
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

            //Set the File Path.
            string filePath = HttpContext.Current.Server.MapPath("~/Files/") + fileName;

            //Check whether File exists.
            if (!File.Exists(filePath))
            {
                //Throw 404 (Not Found) exception if File not found.
                response.StatusCode = HttpStatusCode.NotFound;
                response.ReasonPhrase = string.Format("File not found: {0} .", fileName);
                throw new HttpResponseException(response);
            }

            //Read the File into a Byte Array.
            byte[] bytes = File.ReadAllBytes(filePath);

            //Set the Response Content.
            response.Content = new ByteArrayContent(bytes);

            //Set the Response Content Length.
            response.Content.Headers.ContentLength = bytes.LongLength;

            //Set the Content Disposition Header Value and FileName.
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = fileName;

            //Set the File Content Type.
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(fileName));
            return response;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM