我们已经在iIS8.5服务器上托管了一个asp.net (C#)应用程序。
该应用程序用于下载或预览word文件,并且在一段时间内是工作文件。但是在重启之后它就停止了。
通常,当这种情况发生时,我们只需关闭word文件的所有实例,然后iis就可以开始工作了。但今天,即使在重启之后,它也停止了。
它没有挂起,但当我点击链接进行预览或下载时,它会显示正在等待网站url。但该页面仍然具有响应能力。
当我检查服务器的任务管理器时,我可以看到一个word进程(Microsoft Word (32位))正在运行,当我关闭它时,它会停止网站的运行。
事件查看器没有显示太多关于这方面的内容。我使用了edge和chrome,但没有用到。
这里我们使用预览文档的代码片段
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);
发布于 2019-09-09 06:13:35
使用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;
}
}
https://stackoverflow.com/questions/57848189
复制相似问题