gpt4 book ai didi

c# - 提供存储在服务器上而不是应用程序文件中的图像的路径

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:22 24 4
gpt4 key购买 nike

我需要在将存储在服务器上的 ASP.NET MVC 页面上显示图像我有一个 apphelper 类,我可以用它来提供路径

像这样

public static class AppHelper
{
public static string ImageLowResPath(string imageName)
{

}
}

这里如何获取到服务器c:盘的文件路径?

在我看来我会得到这样的文件路径

img src='<%=AppHelper.ImagelowResPath("10-1010.jpg") %>'

谢谢

最佳答案

如果文件在您的 wwwroot 之外,您必须创建一个返回 FileStreamResult 的操作。

例如。

    public FilestreamResult GetPicture(string Filename) { 
Filename = @"C:\SomePath\" + Filename;
return new FileStreamResult(new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read), "image/jpeg"));
}

你的 html 现在应该是这样的

<img src="/Controller/GetPicture?Filename=test.jpg" />

更新只要您的图像是不经常更改的静态内容,并且您不需要实现某种访问控制,这确实不是最佳解决方案。

根据最佳实践,您应该将组件拆分到多个域中。雅虎发布了关于加速网站的最佳实践的优秀指南 http://developer.yahoo.com/performance/rules.html#split

关于c# - 提供存储在服务器上而不是应用程序文件中的图像的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3063021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com