C# ASP.NET Write File to Client

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



Leave a reply



Submit