For getting list of all Files you can use below function.
public ActionResult GetFiles(string LogPath)
{
DirectoryInfo _directoryInfo = _directoryInfo = new DirectoryInfo(LogPath);
List<SelectListItem> _lstLogFiles = new List<SelectListItem>();
string _ErrorLogFileExtensions = Functions.GetConfigValue(ArchiveConstants.ErrorLogFileExtensions);
string[] _ErrorLogFileExtensionsArr = !string.IsNullOrEmpty(_ErrorLogFileExtensions) ? _ErrorLogFileExtensions.ToLower().Split(',') : new string[0];
if (_directoryInfo.Exists)
{
foreach (FileInfo _file in _directoryInfo.GetFiles().OrderByDescending(x => x.CreationTime))
{
if (_ErrorLogFileExtensionsArr.Contains(_file.Extension))
_lstLogFiles.Add(new SelectListItem { Text = Path.GetFileNameWithoutExtension(_file.Name), Value = _file.Name });
}
}
return Json(_lstLogFiles, JsonRequestBehavior.AllowGet);
}
For Downloading File you can use following funciton.
public ActionResult GetFileContent(string FileName, string LogPath)
{
try
{
string _FilePath = Path.Combine(LogPath, FileName);
byte[] fileBytes = System.IO.File.ReadAllBytes(_FilePath);
Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
catch (Exception ex)
{
return Json("Error while reading file, Please check file path", JsonRequestBehavior.AllowGet);
}
}
Create both action method where you need it. and also you need to set permission iisuser will able to access that folder.