【问题标题】:How to fix 'Not Allowed to load local resource' in asp.net mvc如何修复 asp.net mvc 中的“不允许加载本地资源”
【发布时间】:2019-08-06 09:07:37
【问题描述】:

我已经在C盘上传了我的asp.net应用程序,我在D盘上传了所有资源文件,它可以上传任何类型的文件,但是当我想显示文件时,浏览器无法访问文件因为安全。 现在我的问题是如何解决这个问题,因为我不想在我的应用程序文件夹中上传任何类型的文件。

非常感谢

我已尝试使用 HTML 图像标记来显示文件。

我的期望是显示文件,如果有图像,然后显示图像,如果没有,则显示默认图像,但用户可以下载。

错误消息:不允许加载本地资源。

【问题讨论】:

标签: c# asp.net model-view-controller


【解决方案1】:

为了获取所有文件的列表,你可以使用下面的函数。

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);
        }

对于下载文件,您可以使用以下功能。

 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);
            }
        }

在需要的地方创建这两种操作方法。您还需要设置权限 iisuser 才能访问该文件夹。

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 2023-03-20
    • 2019-05-24
    • 1970-01-01
    • 2016-10-08
    • 2016-11-04
    • 2014-12-10
    相关资源
    最近更新 更多