I'm wanting to leverage Response.WriteFile()
to send a file to the client machine but when I implement it inside of a controller, my return View()
is no longer returning a View. My guess is that Response is already responding to the request. How would you leverage Response.WriteFile()
so that it returns the file but so that I can still return my View? I thought about using StreamWriter
but the path needs to be based on what the users browser is setup as. Any suggestions? The objective of this is to test how long a given user takes to download a file, a connectivity test.
public ActionResult DownloadTest()
{
SpeedTest Test = new SpeedTest();
Services.IPAddress ip = new Services.IPAddress();
var clientIP = ip.GetIPAddress();
string[] IPAddresses = clientIP.Split(':');
Test.Address = IPAddresses[0];
double speed = 0.00;
try
{
double fileSize = 2.67; //Size of File in MB.
DateTime startTime = DateTime.Now;
//Response.ContentType = "image/JPEG";
//Response.AddHeader("Content-Disposition", "attachment;filename=DownloadTest.jpg");
//Response.WriteFile(Server.MapPath("~/TestFile/2point67mb.jpg"));
DateTime endTime = DateTime.Now;
speed = Math.Round((fileSize / (endTime - startTime).TotalSeconds));
Test.ResponseTime = string.Format("{0} Mbps", speed);
Test.Status = "Success";
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
db.SpeedTest.Add(Test);
db.SaveChanges();
}
catch
{
Test.ResponseTime = "N/A";
Test.Status = "Failed";
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
db.SpeedTest.Add(Test);
db.SaveChanges();
}
return View(Test);
}