ImageUrl 尝试显示项目根目录之外的图像

发布于 2024-08-16 11:57:13 字数 329 浏览 4 评论 0原文

我正在使用 ASP.NET 中的两个不同的网站。在第一个项目中,我将一些图像上传到项目根目录下的特定文件夹,并仅将文件名保存在数据库中,现在我尝试在第二个项目的某些页面上显示这些图像,我从数据库和图像中知道文件名文件夹作为绝对 pat 但我无法显示图像,即使在 firebug 中查看图像 src 时认为图像 src 是正确的 src="D:/MyFolder/image.jpg" 图像不显示,可能是因为它没有指向正确的目录。 我也尝试过使用 Server.MapPath 然后使用我的 D 位置,但仍然没有成功。

我确信有人以前遇到过同样的情况,并且真的希望得到一些提示来解决这个问题。 先感谢您

I am working with two different web sites in asp.net. In the first project i upload some images to a specific folder under the project root and save just the filename in the database, now i am trying to display this images at some page of the second project I know the filename from the database and the image folder as absolute pat but I have not been able to display the image, even thought when looking in firebug the image src is correct src="D:/MyFolder/image.jpg" the image does not display, probably because it is not pointing in the right directory.
I have also tried using Server.MapPath and then my D location but still no success.

I am sure someone has faced the same situation before and was really hoping to get some hint to manage this.
Thank you in advance

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

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

发布评论

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

评论(4

月竹挽风 2024-08-23 11:57:13

我找到了我的解决方案,很奇怪,但我之前没有发现。项目下上传的图片始终可以使用项目的 url http://www.yourwebsite.com/images/photo.png 进行访问,现在在第二个项目中,您可以使用此引用图像url 并连接我存储在数据库中的文件名。我认为这是最好的解决方案,并且无需更改代码访问安全性,我认为这可能会带来其他问题。不管怎样谢谢你们。

I found my solution, strange but i didn't catch it before. Uploaded pictures under a project can always be accessed using the url of the project http://www.yourwebsite.com/images/photo.png now in the second project you can use reference the images using this url and concatenating the file name which i store on database. I think this is the best solution and without changing the code access security which i think can bring other problems with it. Anyway thank you guys.

小忆控 2024-08-23 11:57:13

如果您想显示项目中没有的图像(我的意思是它存在于其他项目或其他驱动器中),只需在 IIS 中创建虚拟目录

  1. 转到“运行”,输入 inetmgr
  2. 右键单击您的项目并添加虚拟目录
  3. 提供别名和路径,以便它就像项目中的文件夹一样

If you want to display the image that is not in your project (I mean it is present in some other project or some other drive) just create the virtual directory in IIS

  1. Go to "Run", type inetmgr
  2. Right click on your project and add virtual directory
  3. Give alias name and path so that it acts like folder in your project
怪我闹别瞎闹 2024-08-23 11:57:13

我认为默认情况下您无法在应用程序路径之外提供文件。这称为代码访问安全。您可以在这里阅读:
http://msdn.microsoft.com/en-us/library/930b76w0。 aspx

您可以通过在 web.config 中将信任级别更改为“高”来解决此问题:
http://msdn.microsoft.com/en-us/library/tkscy493。 aspx

我不建议对任何可从外部访问的网站执行此操作。事实上,根据您托管应用程序的方式/地点,此选项可能会受到限制。

I don't think you can serve files outside of your application path by default. It's called Code Access Security. You can read up on it here:
http://msdn.microsoft.com/en-us/library/930b76w0.aspx

You can fix this by changing your trust level to High in your web.config:
http://msdn.microsoft.com/en-us/library/tkscy493.aspx

I wouldn't recommend doing this for any site that is externally accessible. In fact, depending on how/where you're hosting your application, this option may be restricted.

隐诗 2024-08-23 11:57:13

您只能“链接”到相对于同一项目存在的文件或通过绝对 URL 托管在另一个站点上的文件。

如果您想在应用程序/网站之外(在磁盘上或数据库中)提供文件服务,您将需要构建一种机制来获取文件和二进制文件并将其写入浏览器,设置 MIME 类型等。这最好使用HttpHandler。

You can only "link" to files that exist relative to the same project or are hosted on another site via an absolute URL.

If you want to service files outside the application/website (on disk or in a database) you will need to build a mechanism that gets the file and binary writes it to the browser, setting the MIME type etc. This is best done using an HttpHandler.

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