ASP.net MVC 目录浏览

发布于 2024-09-24 06:02:45 字数 357 浏览 1 评论 0原文

我添加了将图像上传到我的 ASP.net mvc 应用程序的功能。基本上,用户选择一个图像,然后将其上传到我在解决方案文件夹结构中创建的图像文件夹。这效果很好,图像会上传到图像文件夹。

我现在遇到的问题是将图像显示回浏览器。我的问题是如何更改 Asp.net 开发服务器 (localHost:blah blah) 的目录浏览属性,以便图像目录即 http://localHost:9823/Images/UploadedImageName.jpg 显示在浏览器中?

提前致谢

Im adding the ability to upload images to my ASP.net mvc application. Basically the user selects an image and I upload it to an Images folder which i've created in my solution folder structure. This works great and the image uploads to the images folder.

The problem I have is now displaying the image back to the browser. My question is how do I change directory browsing properties of the Asp.net Development Server (localHost:blah blah) so that the images directory i.e. http://localHost:9823/Images/UploadedImageName.jpg shows up in the browser?

Thanks in advance

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

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

发布评论

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

评论(1

马蹄踏│碎落叶 2024-10-01 06:02:45

我用过的几个选项。每种风格都有优缺点!

在视图中

您可以将视图配置为使用此样式:

 <img src="<%=ResolveUrl("~/Images/foo.png")" %> />

这可以通过多种方式进行重构。也许是一个扩展方法,或者是视图背后的代码。

控制器方法+路由

创建一个新的控制器来提供图像。确保您有一条默认路由。

 public class Images
 {
   public FileResult GetImage(string fileName) //foo.png
    {
        string fullPath = "~/Images/" +  fileName;
        return base.File(fullPath, "image/jpeg");
    }
 }

...
routes.MapRoute("UserImage",
                "Image/{filename}",
                new { controller = "Images", action = "GetImage", 
                      fileName=filename });

A couple options that I've used. Pros and cons to each style!

In The View

You could have your Views configured to use this style:

 <img src="<%=ResolveUrl("~/Images/foo.png")" %> />

This could be refactored a handful of ways. An extension method perhaps, or codebehind your View.

Controller Method + Route

Create a new Controller to serve up images. Ensure you have a route with a default.

 public class Images
 {
   public FileResult GetImage(string fileName) //foo.png
    {
        string fullPath = "~/Images/" +  fileName;
        return base.File(fullPath, "image/jpeg");
    }
 }

...
routes.MapRoute("UserImage",
                "Image/{filename}",
                new { controller = "Images", action = "GetImage", 
                      fileName=filename });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文