简体   繁体   English

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

[英]Images that are in App_Data folder not shown in browser

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.当我将图像 URL 属性设置为 App_Data 文件夹中的 asp 图像控件时,图像显示在页面设计视图中但不在浏览器中。

<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.这看起来很简单,但它没有显示图像。

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. App_Data文件夹是为数据库文件等数据保留的特殊文件夹,不会在网络上呈现任何内容。 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.您的图像绝对不属于App_Data子文件夹 - 将它们放入/images文件夹或更合适的文件夹中。

Images should never be stored in the App_Data Folder.图像不应存储在App_Data文件夹中。 This is reserved for files that should never be served to the user directly, such as .mdb database files, etc.这是为不应该直接提供给用户的文件保留的,例如 .mdb 数据库文件等。

I would create a /Resources or /Resources/Images folder off the root of the site.我会在站点的根目录下创建一个/Resources/Resources/Images文件夹。

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.在 App_Data 文件夹中隐藏图像并创建自己的 http 处理程序时,您可以保护图像并可以在显示图像之前在图像上添加版权文本等。

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.当我有高分辨率图片时,我会这样做,我不想让每个人都得到,并且让 http 处理程序缩小图像的比例并添加一些版权文本。 Great!伟大的!

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.好的,是时候做不可能的事情了...虽然您不能直接从 app_data 文件夹加载图像,但您可以编写自己的 http 处理程序,它将从 app_data 文件夹读取图像文件并将其发送回客户端。 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!!!但这是不好的做法,如果你为我工作,你会立即被解雇!!!

It depends!这取决于! ;) ;)

There are good reasons for saving images in App_Data .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.部署应用程序时,您可以通过在部署配置中启用“从 App_Data 中排除文件”来保护每个服务器实例的用户上传的这些文件。

If you want to access these files by url use a download handler, downloadfile.ashx for instance.如果您想通过 url 访问这些文件,请使用下载处理程序,例如 downloadfile.ashx。

Hope this helps.希望这可以帮助。

Contents from App_Data folder can be served but not directly. App_Data 文件夹中的内容可以提供,但不能直接提供。
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.将图像存储在全局或本地资源文件夹中,这些也是特殊文件夹,可以以编程方式访问内容。

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}" />

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如果您使用 IIS 管理工具并转到内容过滤(对于您的应用程序),则有一个隐藏段选项卡,您可以在其中删除 App_Data

You'll notice this adds to Web.Config inside the "<system.webServer>" node:您会注意到这会添加到 "<system.webServer>" 节点内的 Web.Config 中:

<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).这样做的一个正当理由是,如果您使用 WebDeploy Publishing 并且想要一种简单的方法来设置它以在目的地删除其他文件,不包括 App_Data 文件夹中的文件(假设您没有将项目中的任何内容发布到 App_Data 并且不在服务器端将任何内容保密)。

在此处输入图片说明

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? )将 WebDeploy 配置为在使用 UI 客户端时不删除其他特定文件夹似乎至少有问题(请参阅如何在发布期间跳过文件夹上的删除?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM