0

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

SQL Server file path

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • So you are uploading a file to the webserver and then you want to provide this file as a download to other users but not from inside the webroot - correct? If so you could try to [stream the file like in this so post](https://stackoverflow.com/questions/3206682/stream-file-using-asp-net-mvc-filecontentresult-in-a-browser-with-a-name) – dsdel Apr 13 '18 at 04:59
  • Thank you dsdel I was able to resolve the issue – Laxman Rathod Apr 13 '18 at 05:39

1 Answers1

0

For completing this request - the solution is based on this comment:

So you are uploading a file to the webserver and then you want to provide this file as a download to other users but not from inside the webroot - correct? If so you could try to stream the file like in this so post: Stream file using ASP.NET MVC FileContentResult in a browser with a name?

dsdel
  • 982
  • 1
  • 6
  • 10