I want to upload a .pdf
file from a webform and then provide a download link for a user to download. I have already written a code in controller and view for uploading a file and saving the physical path of file into SQL Server database and is working fine. Now I am facing a issue in providing a download link for a user so that on clicking on that link it should download the file or open. I tried creating <a></a>
but it will be used to provide link to file which are existing on web but i want to provide link to file which exists on the local machine.
Code for upload file is as below
Controller :
HttpPostedFileBase file = Request.Files["file1"];
try
{
if (file.ContentLength > 0)
{
var filename = System.IO.Path.GetFileName(file.FileName);
var Path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), dbo_tblTdApplicationDetails.ApplicationId.ToString() + ".pdf");
file.SaveAs(Path);
dbo_tblTdApplicationDetails.Path = Path.ToString();
db.Entry(dbo_tblTdApplicationDetails).State = EntityState.Modified;
db.SaveChanges();
}
}
catch
{
ViewBag.Message = "File upload failed!!";
}
View :
<td>
<div class="form-group ">
Upload file
<div class="col-md-10">
<label for="file1">Filename:</label>
<input type="file" name="file1" />
</div>
</div>
</td>
Physical path stored in SQL Server database
C:\....\App_Data\1047.pdf