3

I am using a byte[] to get a serialized version of a file that I want to upload to my ASP.NET website. I am using HttpPostedFileBase on your view model to hold the uploaded file

public class MyViewModel
{
    [Required]
    public HttpPostedFileBase File { get; set; }
}

My HomeController is

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        byte[] uploadedFile = new byte[model.File.InputStream.Length];
        model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

        // Where do I write the file to?

        return Content("Upload complete");
    }
}

and finally in my view I have

@model MyViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(x => x.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
        @Html.ValidationMessageFor(x => x.File)
    </div>
    <button type="submit">Upload</button>
}

The uploads will only be performed by me (the Administrator), and there will only be two, both application installers (.exe files, one 8MB and the other 18MB). My question is how/where can I store the uploaded files so that users can download these?

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    Technically in most of the cases it's always much better to store uploaded files OUTSIDE of the webroot, and access them through an Handler. The good thing with that approach is not only the security, but also the possibility to do additional tasks when file is downloaded (logging, ...) or even the ability to have a permanent link even though you change the file path... – Laurent S. Nov 06 '14 at 14:11
  • Thanks for your time. I take it storing the raw data in a database is not the way to go? Also, using [this](https://www.google.co.uk/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0CAcQjRw&url=http%3A%2F%2Fvikung-fu.deviantart.com%2Fart%2FBartman-The-Dark-Knight-91611234&ei=lI9bVL6vIIajgwTi7IK4AQ&bvm=bv.78677474,d.ZGU&psig=AFQjCNHFlPYcpR5ehl8nPccVP82C_CvmEw&ust=1415373064457448) as your Avitar would be awesome! – MoonKnight Nov 06 '14 at 15:12
  • 1
    Well storing it in the database is also a possibility indeed offering the same advantages as you also need to access it through a handler. DB storage is usually more expensive than file storage though, so you might want to keep the size of stored data quite low. That said, the big advantage of both those solutions is that you can decide to switch to any other solution afterwards, updating your handler will make the change transparent towards your users... – Laurent S. Nov 07 '14 at 08:05

1 Answers1

1

Typically you would use the App_Data folder to do this. This would be accessed with HttpContext.Current.Server.MapPath("~/App_Data/");.

However there are concerns about uploading storing data within the subfolders of a web app (which may well be lessened by the design of vNext). These days anything like this (that users might want to access via the internet) I would store on something like Azure Storage or S3 which have some cost but it is so low as to not be an issue in most cases (~$0.0300 per GB per month).

Community
  • 1
  • 1
AlexC
  • 10,676
  • 4
  • 37
  • 55
  • Thanks for your time. Before you answered I was starting to create a `DownloadsDbContext : DbContext` in which to store the files as a `byte` array. Presumably from your answer, this is a bad idea? Sorry about the poor standard of questions, I am very new to web-development and have no one to ask. Finding answers to simple design queries is very difficult... – MoonKnight Nov 06 '14 at 15:10
  • You could and it is not that bad an idea but I wouldn't generally. A traditional database is not a very good place to store files see http://programmers.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database . The cost of db storage is usually more expensive than file storage. Modern dbs offer the ability to do hybrid file / table storage like SQL servers FileTables - but that may well be overkill for what you are looking at. – AlexC Nov 06 '14 at 15:22
  • Thanks. Yeah, all I need to provide our users with is the ability to down load at very most, four different install packages (ranging from 8MB up to about 18MB). So you are saying that I can get the servers map path using `HttpContext.Current.Server.MapPath("~/App_Data/");` but could you provide any further information on how I can then write my `byte` array to this location? Thanks again for your time... – MoonKnight Nov 06 '14 at 15:45
  • Try model.File.SaveAs(path); using the HttpPostedFileBase.SaveAs Method http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.saveas(v=vs.110).aspx – AlexC Nov 06 '14 at 15:52