C# 如何在System.Web.HttpContext.Current.Response.WriteFile()中写入空白文件
如何在C#响应对象中写入空白文件C# 如何在System.Web.HttpContext.Current.Response.WriteFile()中写入空白文件,c#,asp.net,C#,Asp.net,如何在C#响应对象中写入空白文件 byte[] buffer = new byte[1]; Response.Clear(); Response.ContentType = "text/plain"; Response.OutputStream.Write(buffer, 0, buffer.Length); Response.AddHeader("Content-Disposition", "attachment;
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
我想写一个路径根本不存在的文件
System.Web.HttpContext.Current.Response.WriteFile("blank.txt")
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
我怎么做?你不能。您可以做的是,创建一个空文件,将其命名为“blank.txt”,并将其保存在您的解决方案中
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
我能知道写空白文件的原因吗。这不等于不写任何回应吗
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
更新
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
基于Jon的想法,您甚至可以简单地编写HTTP头
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length","0");
Response.Flush();
Response.End();
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
更新2
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
如果您已经有errorLog.txt文件,请直接使用Response.WriteFile您不能。您可以做的是,创建一个空文件,将其命名为“blank.txt”,并将其保存在您的解决方案中
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
我能知道写空白文件的原因吗。这不等于不写任何回应吗
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
更新
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
基于Jon的想法,您甚至可以简单地编写HTTP头
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length","0");
Response.Flush();
Response.End();
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
更新2
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
如果已经有errorLog.txt文件,请直接使用Response.WriteFile查看为非空文件发送的标题,然后自己简单地写出相同的标题(当然,更改内容长度),但不包含任何数据
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
(我假设
WriteFile
确实在文件名周围填充了一些标题等。如果没有,则更简单:)查看为非空文件发送的标题,然后自己简单地写出相同的标题(当然,更改内容长度),但不包含任何数据
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
(我假设
WriteFile
确实会在文件名周围填充一些标题等。如果没有,则更简单:)您可以这样做
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
你可以这样做
byte[] buffer = new byte[1];
Response.Clear();
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=blank.txt");
Response.End();
你为什么要这么做?如果文件不存在,您应该抛出一个HttpException(404),我正在使用downloader。如果文件不存在,则需要显示一个默认文件,如errorLog.txt–为什么要这样做?如果文件不存在,您应该抛出一个HttpException(404),我正在使用downloader。如果文件不存在,则需要显示一个默认文件,如errorLog.txt–我正在使用downloader。如果文件不存在,则需要显示一个默认文件,如errorLog.txtResponse.AddHeader(“内容长度”,0);遇到语法错误,响应的最佳重载方法匹配。AddHeader(“content length”,0)有一些无效参数。上面的代码在errorLog.txt文件中没有内容。所以它遇到了损坏的内容错误实际上我需要使用System.IO.file.create()方法创建一个文件如果你可以只写一个空格呢?现在我正在使用这段代码。。字符串_str=Path.Combine(根,“errorLog.txt”);FileStream fs=File.Create(_str);Byte[]info=new System.Text.UTF8Encoding(true).GetBytes(“错误:服务器上不存在请求的文件”);fs.Write(信息,0,信息长度);fs.Close();System.Web.HttpContext.Current.Response.WriteFile(_str);我正在使用下载器。如果文件不存在,则需要显示一个默认文件,如errorLog.txtResponse.AddHeader(“内容长度”,0);遇到语法错误,响应的最佳重载方法匹配。AddHeader(“content length”,0)有一些无效参数。上面的代码在errorLog.txt文件中没有内容。所以它遇到了损坏的内容错误实际上我需要使用System.IO.file.create()方法创建一个文件如果你可以只写一个空格呢?现在我正在使用这段代码。。字符串_str=Path.Combine(根,“errorLog.txt”);FileStream fs=File.Create(_str);Byte[]info=new System.Text.UTF8Encoding(true).GetBytes(“错误:服务器上不存在请求的文件”);fs.Write(信息,0,信息长度);fs.Close();System.Web.HttpContext.Current.Response.WriteFile(_str);好主意。我已经更新了我的答案以反映你的观点+这是个好主意。我已经更新了我的答案以反映你的观点+1.