ImageUrl 尝试显示项目根目录之外的图像
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了我的解决方案,很奇怪,但我之前没有发现。项目下上传的图片始终可以使用项目的 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.如果您想显示项目中没有的图像(我的意思是它存在于其他项目或其他驱动器中),只需在 IIS 中创建虚拟目录
inetmgr
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
inetmgr
我认为默认情况下您无法在应用程序路径之外提供文件。这称为代码访问安全。您可以在这里阅读:
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.
您只能“链接”到相对于同一项目存在的文件或通过绝对 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.