22

我在 IIS 7 共享主机环境中有一个网站。它正在运行 .NET 3.5。我有一个下载按钮,可以从服务器下载文件。

当我将此应用程序本地部署到 IIS 6 时,它运行良好。在 IIS 7 共享主机服务器上,发生异常。

句柄无效。(来自 HRESULT 的异常:0x80070006 (E_HANDLE)) 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
System.Runtime.InteropServices.COMException:句柄无效。(来自 HRESULT 的异常:0x80070006 (E_HANDLE))
COMException (0x80070006):句柄无效。(来自 HRESULT 的异常:0x80070006 (E_HANDLE))] [HttpException (0x80004005):与远程主机通信时出错。错误代码为 0x80070006。]

如何解决?

string strRequest = Convert.ToString(Request.QueryString.Get("file"));
System.IO.FileInfo file = new System.IO.FileInfo(strRequest);
if (file.Exists)
{
    Response.Clear();
    Response.ContentType = ReturnExtension(System.IO.Path.GetExtension(file.Name));
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.TransmitFile(strRequest);
    Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    //DownloadFile(file.FullName, file.Name);
}
4

6 回答 6

14

Create a .bat file, put the following command and run the file. It will kill all existing webserver processes and should fix the problem. I had the same problem and it worked out for me. Thanks much

taskkill  /fi "imagename eq webdev.webserver40.exe" 
于 2012-12-18T15:13:06.477 回答
13

我从下面的链接中找到了修复:

http://forums.asp.net/t/1387967.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

if (file.Name == fileName)

{
     Response.ClearContent();
     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
     Response.AddHeader("Content-Length", file.Length.ToString());
     Response.TransmitFile(file.FullName);
     //Response.End(); Will raise that error. this works well locally but not with IIS
     Response.Flush();//Won't get error with Flush() so use this Instead of End()


}
于 2013-11-19T19:48:05.147 回答
3

我刚刚在我们的环境中解决了这个问题。我们打开了模拟,并让应用程序池作为 ApplicationPoolIdentity 运行。

问题是由于应用程序池标识没有对源文件的读取权限,即使模拟用户确实有权访问该文件。解决这个棘手的问题是,如果用户和应用程序池都没有访问权限,则会出现访问权限错误。

于 2015-08-17T13:21:35.270 回答
2

编辑:最初错过了有关页面加载正常的部分。我不确定从您的查询字符串中传递了什么,但是您是否尝试过使用 Server.MapPath?所以而不是

System.IO.FileInfo file = new System.IO.FileInfo(strRequest);

你有

System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(strRequest));

让我知道这是否有帮助。

于 2011-10-12T18:24:02.700 回答
2

就我而言,我试图写入和读取这个文件:

var path = System.IO.Path.GetTempFileName();

我使用了下面的代码并且它有效。我认为 IIS 用户缺少写入或读取临时文件的权限。

var path = Server.MapPath(@"~\App_Data\Stats");
Directory.CreateDirectory(path);
path = Path.Combine(path, String.Format("{0}.csv", Guid.NewGuid()));

using (var streamWriter = new StreamWriter(path))
using (var csvWriter = new CsvHelper.CsvWriter(streamWriter))
{
    csvWriter.Configuration.Delimiter = csvWriter.Configuration.CultureInfo.TextInfo.ListSeparator;

    csvWriter.WriteRecords(rounds);
}

return File(path, "text/csv", "Stats.csv");
于 2014-03-26T14:46:28.987 回答
0

就我而言,这仅发生在特定用户登录时。每个其他用户都可以正常工作。

问题是用户的 LoginEmail 中有一个额外的空白。

这发生在一个 MVC 应用程序中,该应用程序使用 Asp.net Identity 和 Impersonation 从托管服务器上的目录下载 excel 文件。

奇怪的东西!

于 2018-04-06T14:57:13.630 回答