我在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);
}
发布于 2014-03-26 14:46:28
在我的例子中,我试图写和读到这个文件:
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");
https://stackoverflow.com/questions/7739543
复制相似问题