I have uploaded a file into my database previously, now I want to download that file from my database.
Can anyone show me how? I am new to C# and ASP.NET MVC.
Controller :
public ActionResult Details(string id = null)
{
Assignment assignment = db.Assignments.Find(id);
if (assignment == null)
{
return HttpNotFound();
}
return View(assignment);
}
Model:
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public byte[] FileLocation { get; set; }
View:
<div class="display-label">
<%: Html.DisplayNameFor(model => model.FileLocation) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.FileLocation) %>
</div>
Since you have the a Stream
or byte[]
or a path of a file, you can use the File()
method that comes from Controller
base class, and respond this file as an ActionResult
, for sample:
public ActionResult Details(string id = null)
{
Assignment assignment = db.Assignments.Find(id);
if (assignment == null)
{
return HttpNotFound();
}
return File(assigment.FileLocation, "content/type", "fileName.extension");
}
Remember to get the right content-type
and add a fileName
with the correct extension.