PROWAREtech

articles » archived » asp-net » mvc » download-files

ASP.NET: MVC Download Files

How to download files from the MVC controller (.NET Framework).

Download any file type from a controller in ASP.NET MVC using FileStreamResult. Note: if using ASP.NET Core, see this page, if wanting to upload files to the server, see this page.

// download a zip file as an attachment
public FileStreamResult DownloadZipFile()
{
	string path = Server.MapPath("/files/somefile.zip");
	if (System.IO.File.Exists(path))
	{
		Response.AppendHeader("content-disposition", "attachment;filename=somefile.zip");
		System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!
		return new FileStreamResult(stream, "application/x-zip-compressed"); // the constructor will fire Dispose() when done
	}
	else
		return null;
}
// download a jpeg as an attachment
public FileStreamResult DownloadJpegFile()
{
	string path = Server.MapPath("/files/someimage.jpg");
	if (System.IO.File.Exists(path))
	{
		Response.AppendHeader("content-disposition", "attachment;filename=someimage.jpg");
		System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!
		return new FileStreamResult(stream, "image/jpeg"); // the constructor will fire Dispose() when done
	}
	else
		return null;
}
// download a text file as an attachment
public FileStreamResult DownloadTextFile()
{
	string path = Server.MapPath("/files/somefile.txt");
	if (System.IO.File.Exists(path))
	{
		Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");
		System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!
		return new FileStreamResult(stream, "text/plain"); // the constructor will fire Dispose() when done
	}
	else
		return null;
}

Download any data type from a ASP.NET MVC controller using FileContentResult which takes a byte array.

public FileContentResult DownloadTextFile()
{
	byte[] bytes = System.Text.Encoding.ASCII.GetBytes("How now brown cow.");
	Response.AppendHeader("content-disposition", "attachment;filename=sometext.txt");
	return new FileContentResult(bytes, "text/plain");
}

Or use a FileStream to fill the byte array.

public FileContentResult DownloadTextFile()
{
	System.IO.FileStream stream = new System.IO.FileStream("somefile.txt", System.IO.FileMode.Open);
	byte[] bytes = new byte[stream.Length];
	stream.Read(bytes, 0, bytes.Length);
	Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");
	return new FileContentResult(bytes, "text/plain");
}

Or simply:

public FileContentResult DownloadTextFile()
{
	byte[] bytes = System.IO.File.ReadAllBytes("somefile.txt");
	Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");
	return new FileContentResult(bytes, "text/plain");
}

Download from a MemoryStream:

public FileContentResult DownloadFromMemoryStream()
{
	using(System.IO.MemoryStream ms = new System.IO.MemoryStream())
	{
		ms.WriteByte((byte)'a');
		ms.WriteByte((byte)'b');
		ms.WriteByte((byte)'c');
		byte[] bytes = ms.ToArray();
		Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");
		return new FileContentResult(bytes, "text/plain");
	}
}

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT