Benjamin
Benjamin

Reputation: 3234

ASP.NET MVC uploaded images folder inaccessible after deployment

I can navigate directly to this image on the development server and it is displayed.

http://localhost:51122/Uploads/0a2234e6-71c2-4ca4-83b9-4bf9510f25bc

But after publishing I get a 404

http://localhost/hts/Uploads/0a2234e6-71c2-4ca4-83b9-4bf9510f25bc  

Version info:
Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.272

If it is an MVC routing issue then why does it work in development?

EDIT: LOL - because the Uploads folder and its content were not copied over to the deployed app's folder. The path did not exist. I guess I assumed it wouldn't be an issue since the app's database was not an issue.

I had a second problem regarding the mime type because there are no extensions on these jpeg's, just a guid name from swfupload. I created a mime type for . in IIS that is image/jpeg and the images displayed. I imagine I should just add the extension to the filenames.

Upvotes: 0

Views: 628

Answers (1)

Nick Bork
Nick Bork

Reputation: 4841

Since I've worked with SWFUpload before I figured I would give you some of the code I used for my ActionResult. Below is some code that I used. "CAA" is my project namespace, so "CAA.Utility.IO" is my namespace to some helper classes I built which are included below.

    public ActionResult Index()
    {
        if (Request.Files.Count != 0)
        {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < Request.Files.Count; ++i)
            {
                CAA.Utility.IO.IFileStore _fileStore = new CAA.Utility.IO.DiskFileStore(Server.MapPath("~/assets/uploads/temp"));
                sb.Append(_fileStore.SaveUploadedFile(Request.Files[i]));
            }
            return new ContentResult() { Content = sb.ToString(), ContentType = "text/html" };

        }
        else
            return new ContentResult() { Content = "", ContentType = "text/html" };
    }

And my IFileStore class:

using System;
using System.Web;

namespace CAA.Utility.IO
{
    public interface IFileStore
    {
        string SaveUploadedFile(HttpPostedFileBase fileBase);
    }
}

And my IDiskStore class:

using System;
using System.Web;
using System.IO;
using System.Web.Hosting;

namespace CAA.Utility.IO
{
    public class DiskFileStore : IFileStore
    {
        public DiskFileStore() { }
        public DiskFileStore(string UploadFolder)
        {
            this._uploadsFolder = UploadFolder;
        }

        public string UploadFolder
        {
            get
            {
                return _uploadsFolder;
            }
            set
            {
                _uploadsFolder = value;
            }

        }
        private string _uploadsFolder = HostingEnvironment.MapPath("~/assets/uploads/temp");

        public string SaveUploadedFile(HttpPostedFileBase fileBase)
        {
            int lastPeriod = fileBase.FileName.LastIndexOf(".");
            string fileExtension = "";
            if (lastPeriod != -1)
            {
                fileExtension = fileBase.FileName.Substring(lastPeriod);
            }

            var identifier = Guid.NewGuid();
            fileBase.SaveAs(GetDiskLocation(identifier, fileExtension));
            return identifier.ToString() + fileExtension;
        }

        private string GetDiskLocation(Guid identifier, string FileExtension)
        {
            return Path.Combine(UploadFolder, identifier.ToString() + FileExtension);
        }
    }
}

You'll notice that when you create an instance of the DiskFileStore it takes the path you want to save the files to. If you don't specify, it has a default file location. The method will return a string with a GUID and extension.

Upvotes: 1

Related Questions