Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written this code to query my database for the dynamic path created which works.

C#
public List<MyCombinedQueryModel> Files(int? id)
         {
             

              var uploads = (from u in _db.Patients
                            where u.PatientsId == id
                            select u.FilesUrl).FirstOrDefault();


              if (uploads != null)
              {
                   var dir = new DirectoryInfo(uploads);

                   IEnumerable<FileInfo> filelist = dir.EnumerateFiles("*.*", SearchOption.AllDirectories);

                   var query = (from file in filelist
                                where file.Length > 0
                                let fileName = file.Name
                                select new MyCombinedQueryModel
                                            {
                                                 FileName = file.Name
                                            }).ToList();


                   return query; 
              }
              return null;
         }



My MVC Html display iterates and list all files in the chosen filed folder.

HTML
<div class="display-label">Uploaded Documents</div>
          
          <div class="display-field">
               <tr>
                    <th>FileName</th>
                    <th>Download Files</th>
               </tr>
               
              
                    @foreach (var file in ((IEnumerable<MyCombinedQueryModel>) ViewBag.Files))
                    {<tr>
                          <td>
                               @Html.ActionLink("Download", "Download", new { id = Model.PatientsId, file.FileName })
                          </td>
                          <td>
                           @file.FileName
                          </td>
                     </tr>
                         
                    }
              </div>


Now I am trying to create code to allow user to download files by selection.
if the above c#, mvc html finds six files. Whatever file I choose with the link download I need to download those files.

The @file.FileName is grayed out and read only.

C#
public ActionResult Download(int id)
         {
              
              
              var uploads = (from u in _db.Patients
                            where u.PatientsId == id
                            select u.FilesUrl).FirstOrDefault();


              if (uploads != null)
              {
                   string folder = Path.GetFullPath(uploads);
                   return File(folder, "", "");
              }

         }
Posted

1 solution

problem solved thanks

HTML
<div class="display-label">Uploaded Documents</div>
          
          <div class="display-field">
               <tr>
                    <th>FileName</th>
                    <th>Download Files</th>
               </tr>
               
              
                    @foreach (var file in ((IEnumerable<mycombinedquerymodel>)ViewBag.Files))
                    {<tr>
                          <td>
                               @Html.ActionLink("Download", "Download", new { id = Model.PatientsId, name = file.FileName })
                          </td>
                          <td>
                            @file.FileName
                           </td>
                     </tr>
                         
                    }
              </mycombinedquerymodel></div>


behind code.

C#

C#
public ActionResult Download(int? id, string name)
        {


             string fileName = name;


             var uploads = (from u in _db.Patients
                           where u.PatientsId == id
                           select u.FilesUrl).FirstOrDefault();


             if (uploads != null)
             {

                  string folder = Path.GetFullPath(uploads);
                  //HttpContext.Response.AddHeader("content-dispostion", "attachment; filename=" + fileName);
                  return File(new FileStream(folder +"/" + fileName, FileMode.Open), "content-dispostion", fileName);
             }

             throw new ArgumentException("Invalid file name of file not exist");
        }
 
Share this answer
 
Comments
Member 10904047 2-Oct-14 14:03pm    
Hi
I am getting undefined on my page. How can I fix it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900