2
votes

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.

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.

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?

2
I've edited you question, see why: meta.stackexchange.com/questions/19190/…Arin Ghazarian
And yet you've made my question less clear. I've now modified the title according to feedback from Jeff Atwood (meta.stackexchange.com/questions/103563/…).Brian Lacy

2 Answers

2
votes

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 );
    }
}

and in your HTML:

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

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

0
votes

i think there are 2 ways to fix it

1.use a tool synchronous files between machines
it makes duplicate files,per machine has same file

2.upload file to a net address like //192.168.1.1/upload,and host a website on iis like img.domain.com,than img url use this domain
file not duplicate,so you should make sure the browser can find it.the img domain not balance

or upload file to a cloud service