0

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.

3
  • Is there a reason you didn't want to preserve the file extension? I ask because a mime type catch all for image/jpeg is not ideal. I just wonder if there is a specific reason or if something caused you to have to do it that way.
    – Nick Bork
    Feb 8, 2012 at 4:25
  • @Splash-X I agree, it sounds like a disaster waiting to happen. The files have no extension because I did not know how to get it from the online examples of swfupload that I was following. I would rather preserve the extension and get rid of the workaround. I'll have to study swfupload and .net file handling some more since I'm new to this stuff.
    – Benjamin
    Feb 8, 2012 at 6:04
  • check out the answer I posted, it has a sample of some methods to accept a collection of files from SWFUpload (if you use the multi-upload) and pass back a file name. It so happens that my code returns a string that is a GUID with a file extension. Thought it might save you some time.
    – Nick Bork
    Feb 8, 2012 at 15:30

1 Answer 1

1

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.

2
  • This is helpful but something feels weird to me about this FileStore approach. Maybe it's just a naming thing throwing me off. I ended up just replacing the original code I had with two lines in my action method. I will refer back to your example here in the future when I make the upload workflow in my app more robust with drag and drop. Thanks! btw swfupload is n/g with windows authentication because of flash.
    – Benjamin
    Feb 23, 2012 at 16:20
  • The FileStore allows you to easily change out where you wish to save your files to. It could be a Disk, a Database, Memory, etc. To use SWFUpload you would need to allow anonymous access to what ever action is handeling your upload. Thats just a limitation of using the Flash object.
    – Nick Bork
    Feb 23, 2012 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.