0

I have uploaded my asp.net application in C drive, and I upload all resource files in D drive, and it's uploaded fine any kinds of file, but when I want to show the file, then Browser can't access the file because of security. Now my question is how can I solve this because I don't want to upload any kind of files in my application's folder.

thank you very much

I have tried the HTML image tag to show the file.

My expectation is to show files if it images then show the image if not then show a default image but the user can download this.

Error Message: Not Allow to load local resources.

Sraban75
  • 85
  • 9

1 Answers1

0

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.

Bhaumik Shah
  • 382
  • 2
  • 8
  • 20