App_Data 文件夹中的图像未显示在浏览器中

发布于 2024-08-06 14:34:46 字数 280 浏览 12 评论 0原文

当我将图像 URL 属性设置为 App_Data 文件夹中的 asp 图像控件时,图像显示在页面设计视图中,但不显示在浏览器中。

<form id="form1" runat="server">
<div>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/App_Data/p3.jpg" />
</div>
</form>

看起来很简单,但它没有显示图像。

When I set image URL property to asp image control that is in App_Data folder, image is showing in page design view but not in browser.

<form id="form1" runat="server">
<div>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/App_Data/p3.jpg" />
</div>
</form>

It seems to be straightforward, but it's not showing the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我的鱼塘能养鲲 2024-08-13 14:34:46

App_Data 文件夹是为数据库文件等数据保留的特殊文件夹,并且不会在网络上呈现任何内容。这是设计使然,是故意的,无法更改(据我所知)。

您的图像绝对不属于 App_Data 子文件夹 - 将它们放入 /images 文件夹或更合适的文件夹中。

The App_Data folder is a special folder reserved for data such as database files and so on, and will NOT render out any contents on the web. This is by design, and intentional and cannot be changed (as far as I know).

Your images do definitely not belong into the App_Data subfolder - put them into a /images folder or something more appropriate.

愛放△進行李 2024-08-13 14:34:46

图像永远不应存储在 App_Data 文件夹中。这是为永远不应该直接提供给用户的文件保留的,例如 .mdb 数据库文件等。

我将创建一个 /Resources/Resources/Images 文件夹离开网站的根目录。

Images should never be stored in the App_Data Folder. This is reserved for files that should never be served to the user directly, such as .mdb database files, etc.

I would create a /Resources or /Resources/Images folder off the root of the site.

伏妖词 2024-08-13 14:34:46

我不同意。将图像隐藏在 App_Data 文件夹中并创建自己的 http 处理程序时,您可以保护图像并可以在显示图像之前在图像上添加版权文本等。

当我有高分辨率图片时我会这样做,我不想让每个人都得到,并且让http处理程序缩小图像并添加一些版权文本。伟大的!

I disagree. When hiding images in the App_Data folder and creating your own http-handler you secure your images and can add copyright-text etc. on the images before you show them.

I do this when I have highres pictures i don't wan't everybody to get, and having the http-handler downscale the image and put on some copyrighttext. Great!

沩ん囻菔务 2024-08-13 14:34:46

好吧,是时候做不可能的事了...虽然您无法直接从 app_data 文件夹加载图像,但您可以编写自己的 http 处理程序,该处理程序将从 app_data 文件夹中读取图像文件并将其发送回客户端。这将是一种解决方法,但一般来说,数据是指只有您的应用程序可以读取的数据。通过让处理程序读取数据,您仍然可以返回这些图像。

但这是不好的做法,如果你为我工作,你会立即被解雇!

Okay, time to do the impossible... While you cannot load images directly from the app_data folder, you can write your own http handler which will read the image file from the app_data folder and send it back to the client. It would be a work-around but in general, the data is meant for data that only your application can read. By having a handler reading the data, you can still return those images.

But it's bad practice and if you'd be working for me, you'd be fired immediately!!!

乱了心跳 2024-08-13 14:34:46

这取决于! ;)

将图像保存在 App_Data 中是有充分理由的。在您的用户可以上传其文件或徽标的情况下,它将保护这些文件,并且不会让其他用户访问它们或将其公开。

最重要的是,这是每个服务器/部署实例拥有不同文件的唯一方法。

部署应用程序时,您可以通过在部署配置中启用“从 App_Data 中排除文件”来保护每个服务器实例的用户上传的这些文件。

如果您想通过 url 访问这些文件,请使用下载处理程序,例如 downloadfile.ashx。

希望这有帮助。

It depends! ;)

There are good reasons for saving images in App_Data. In situations where your users can upload their files or logos it will protect these files and not make them accessible to other users or being public.

Most important, it's the only way having different files per server/deployment instance.

When deploying your app you can protect these files uploaded by users per server instance by enabling "Exclude files from App_Data" in your deployment configuration.

If you want to access these files by url use a download handler, downloadfile.ashx for instance.

Hope this helps.

生生漫 2024-08-13 14:34:46

可以提供 App_Data 文件夹中的内容,但不能直接访问。
无法直接访问,不建议间接访问。这是有意为之。

但是添加虚拟路径可以做到这一点。
请参阅此问题

我认为前三个答案符合您的目的。
将图像存储在全局或本地资源文件夹中,这些也是特殊文件夹,可以通过编程方式访问内容。

Contents from App_Data folder can be served but not directly.
Direct access is not possible and indirect is not recommended. It is intentional.

however adding a virtual path can do this.
See this question

I think top three answer serves your purpose.
Store images in resource folder either global or local these are also special folders and contents can be accessed programaticaly.

独自←快乐 2024-08-13 14:34:46
public string ReturnImage(){
 
alternatively if you wanted to pass a param.

so for example

int WhatEverId = 5;

String folderPath = string.empty;
HostingEnvironment.MapPath("~/App_Data/YourFolder") + @"\" +WhatEverId.ToString());

 string imageYouWantToDisplay = "Test.png";
 string base64String = "";
 String path = HostingEnvironment.MapPath("~/App_Data");
 
 using (Image image = Image.FromFile(path + "/" + imageYouWantToDisplay))
  {
     using (MemoryStream m = new MemoryStream())
      {
        image.Save(m, image.RawFormat);
        byte[] imageBytes = m.ToArray();

        // Convert byte[] to Base64 String
          base64String = Convert.ToBase64String(imageBytes);
      }
    }

    return base64String;

}

然后您可以在操作方法

public DisplayImages (){
List<WhateverModel> test = new List<WhateverModel>();

 test = GetAll().ToList();

  test.ForEach(x=> { MyImage = ReturnImage();});

  return test;

  }

View中调用它

@model WhateverModel
<img src="@MyImage" /> or in js  <img src="${MyImage}" />
public string ReturnImage(){
 
alternatively if you wanted to pass a param.

so for example

int WhatEverId = 5;

String folderPath = string.empty;
HostingEnvironment.MapPath("~/App_Data/YourFolder") + @"\" +WhatEverId.ToString());

 string imageYouWantToDisplay = "Test.png";
 string base64String = "";
 String path = HostingEnvironment.MapPath("~/App_Data");
 
 using (Image image = Image.FromFile(path + "/" + imageYouWantToDisplay))
  {
     using (MemoryStream m = new MemoryStream())
      {
        image.Save(m, image.RawFormat);
        byte[] imageBytes = m.ToArray();

        // Convert byte[] to Base64 String
          base64String = Convert.ToBase64String(imageBytes);
      }
    }

    return base64String;

}

you can then call that in an action method

public DisplayImages (){
List<WhateverModel> test = new List<WhateverModel>();

 test = GetAll().ToList();

  test.ForEach(x=> { MyImage = ReturnImage();});

  return test;

  }

View

@model WhateverModel
<img src="@MyImage" /> or in js  <img src="${MyImage}" />
叹沉浮 2024-08-13 14:34:46

如果您使用 IIS 管理工具并转到内容过滤(针对您的应用程序),则会有一个 隐藏分段 选项卡,您可以在其中删除 App_Data

您会注意到这会添加到“”内的 Web.Config节点:

<security>
    <requestFiltering>
        <hiddenSegments>
            <remove segment="App_Data" /> 
        </hiddenSegments>
    </requestFiltering>
</security>

这样做的一个有效原因是,如果您使用 WebDeploy 发布并希望有一种简单的方法将其设置为删除目标处的其他文件,排除 App_Data 文件夹中的文件(假设您没有将项目中的任何内容发布到 App_Data 中并且不这样做)不要在服务器端保留任何私密内容)。

输入图片此处的描述

配置 WebDeploy 在使用 UI 客户端时不删除其他特定文件夹似乎至少是有问题的(请参阅 如何在发布过程中跳过文件夹删除?

If you use the IIS Administration tool and go to Content Filtering (for your application), there is a Hidden Segments tab where you can remove App_Data

You'll notice this adds to Web.Config inside the "<system.webServer>" node:

<security>
    <requestFiltering>
        <hiddenSegments>
            <remove segment="App_Data" /> 
        </hiddenSegments>
    </requestFiltering>
</security>

A valid reason for doing that is if you use WebDeploy Publishing and want an easy way to set it to remove additional files at destination, excluding files from the App_Data folder (assuming you're not publishing anything from your project into App_Data and don't keep anything private there at the server side).

enter image description here

Configuring WebDeploy to not delete other specific folder when using the UI client seems to be problematic at least (see How to skip delete on folder during publish?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文