如何存储和放大器;检索跨负载平衡服务器上传的图片与ASP.NET? [英] How to store & retrieve uploaded images across Load Balanced servers with ASP.NET?

查看:117
本文介绍了如何存储和放大器;检索跨负载平衡服务器上传的图片与ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET MVC应用程序将被部署到了一系列的负载平衡Web服务器。有一个问题我还在制定是如何处理动态上传的文件内容,例如用户上传的图片 - 显然,节省了他们在那里他们被上传不会允许他们从其他服务器可以访问的服务器上负载均衡组中。

My ASP.NET MVC application will be deployed to a series of load-balanced web servers. One problem I'm still working out is how to handle dynamically-uploaded file content, such as user-uploaded images -- obviously, saving them on the server where they were uploaded won't allow them to be accessed from the other servers in the load balanced group.

目前我打算将这些保存到共享存储位置,特别是一个UNC路径指的是我们的NAS目录;但我不知道如何以最佳方式获取这些文件,将它们展示给客户。我想我需要编写某种自定义的路由处理从服务器端非Web访问的存储位置进行检索,然后他们流返回给客户端。这似乎相对简单的事,但我与如何开始在ASP.NET处理这个苦苦挣扎。

Currently I'm planning to save these to a shared storage location, specifically a UNC path referring to a directory on our NAS; but I'm not sure how best to retrieve these files to display them to the client. I'm thinking I'll need to write a custom route handler of some kind to retrieve them from the non-web-accessible storage location on the server side and then stream them back to the client. This seems relatively straightforward to do, yet I'm struggling with how to begin to approach this in ASP.NET.

我考虑的另一个解决方案是创造一种指向网络目录中的每个应用程序目录的虚拟目录。

Another solution I've considered is creating a Virtual Directory in each application directory which points to the network directory.

我甚至考虑过(通过文件上传处理code)上传文件到Amazon S3和使用CloudFront的交付他​​们,但我宁愿避免外部服务的依赖。

I've even considered uploading the files to Amazon S3 (via the file upload handling code) and using CloudFront to delivery them, but I'd rather avoid the external service dependency.

哪种方法你建议,以及是否有可用于完成这样的事情建立的最佳实践,甚至现有的组件/库?

Which approach do you recommend, and are there established best practices or even existing components/libraries available for accomplishing this sort of thing?

推荐答案

在ASP.NET MVC你可以用一个控制器动作处理这个问题,像这样:

In ASP.NET MVC you can handle this with a controller action, like so:

public class SharedImageController : Controller {

    public ActionResult GetImage(String imageId) {

        String uncPath = GetImageUncLocationFromId( imageId );

        Response.ContentType = "image/jpeg"; // change as appropriate
        return new FileResult( uncPath );
    }
}

和在HTML:

<img src="<%= Url.Action("GetImage", "SharedImage", new  { imageId = "SomeImage.jpg" } %>" alt="Some descriptive text" />

您可以创建自定义的HtmlHelper扩展方法,使这个更不容易出错,如果你将要使用这个有很多。

You could make a custom HtmlHelper extension method to make this less error-prone if you'll be using this a lot.

这篇关于如何存储和放大器;检索跨负载平衡服务器上传的图片与ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆