Reputation: 2197
How can we write a blank file in C# Response Object?
I want to write a file whose path does not exist at all.
System.Web.HttpContext.Current.Response.WriteFile("blank.txt")
how can I do it?
Upvotes: 0
Views: 1688
Reputation: 14915
You can not. What you can do is, make an empty file and name it "blank.txt" and keep it in your solution.
Could I know the reason for writing blank file. It is not equivalent of not writing anything in response ?
UPDATE
Building on Jon's idea, you can even simply write the HTTP headers
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length","0");
Response.Flush();
Response.End();
UPDATE 2
If you already have a errorLog.txt file, use Response.WriteFile directly
Upvotes: 1
Reputation: 570
You can do it this way.
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();
Upvotes: 0
Reputation: 1500215
Look at what headers are sent for a non-empty file, and then simple write out the same headers yourself (changing the content length, of course) but with no data.
(I'm assuming that WriteFile
does fill in some headers around the file name etc. If it doesn't, it's even simpler :)
Upvotes: 2