C# Asp.net write file to client
You could use the Response.ContentType like this
Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.txt");
This of course works if you want to write a text file. In case you want to write a .doc for example you change the ContentType to "application/msword" etc...
ASP C# Send File to Client
It's probably because there is already some output queued in the Response stream ready for transmit. You must first clear it before transmitting your file. If you're using ASP.NET web forms, this will break your page though as the postback behavior will no longer work.
See related SO question and answer.
Creating a text file on the fly and have it download/save on client side
MemoryReader mr = new MemoryStream();
TextWriter tw = new StreamWriter(mr);
foreach (string s in stringList) {
tw.WriteLine(s);
}
tw.Flush();
return File(mr, "application/force-download", "myFile.txt")
Or directly write to the response:
HttpContext.Response.Body.WriteAsync(...);
Also viable alternative
TextWriter tw = new StreamWriter(HttpContext.Response.Body);
so you can directly write to the output string, without any additional memory usage. But you shouldn't close it, since it's the connection to the browser and may be needed further up in the pipeline.
Send file to client and delete it
This snippet should do the trick, but notice that this will result in loading the whole file into the memory (of the server).
private static void DownloadFile(string path)
{
FileInfo file = new FileInfo(path);
byte[] fileConent = File.ReadAllBytes(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", file.Name));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(fileConent);
file.Delete();
HttpContext.Current.Response.End();
}
Passing file from web service to client in ASP.NET
I think the best way to achieve this would be to progressively download the file and buffer it into the response so it progressively downloads to the client. This way, the end user doesn't have to wait for the server to completely download the file and send it back. It will just stream the file content as it downloads it from the other location. As a bonus, you don't have to keep the entire file in memory or on disk!
Here's a code sample achieving this:
protected void downloadButton_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MyFile.exe"));
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
using (var downloadStream = new WebClient().OpenRead("http://otherlocation.com/MyFile.exe")))
{
var uploadStream = Response.OutputStream;
var buffer = new byte[131072];
int chunk;
while ((chunk = downloadStream.Read(buffer, 0, buffer.Length)) > 0)
{
uploadStream.Write(buffer, 0, chunk);
Response.Flush();
}
}
Response.Flush();
Response.End();
}
Related Topics
How to Format Timespan in Xaml
Get Affected Rows on Executenonquery
Show Transparent Loading Spinner Above Other Controls
Net Core: Execute All Dependency Injection in Xunit Test for Appservice, Repository, etc
Select Either a File or Folder from the Same Dialog in .Net
An Expression Tree Lambda May Not Contain a Null Propagating Operator
When Using .Net MVC Radiobuttonfor(), How to Group So Only One Selection Can Be Made
Binding Selecteditems of Listview to Viewmodel
Reversible Shuffle Algorithm Using a Key
How to Programmatically Convert Vb6 Formatting Strings to .Net Formatting Strings
How to Change Listbox Selection Background Color
Making Specific Text Boldefaced in a Textbox
Correlation of Two Arrays in C#
How to Convert a String to Rtf in C#
How to Implement Full Row Selecting in Gridview Without Select Button