How do I upload a file in a custom module?

Topics: Writing modules
Sep 16, 2012 at 2:03 PM

I have a custom module wherein I am trying to upload a file using a plain old form POST with an ActionResult in the controller, but it's not picking it up when I debug.  Fiddler shows the correct url, etc.  What am I missing?  Is there something special in Orchard that prevents this from working as expected?



<form action="/Admin/ABC.InstructorPortal/ManageRosters/UploadFile" enctype="multipart/form-data" method="post">    
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" /> 
    <input type="submit" name="submit" value="Submit" />


        public ActionResult UploadFile(System.Web.HttpPostedFileBase file)
            if (file.ContentLength > 0)
                var fileName = System.IO.Path.GetFileName(file.FileName);
                var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/"), fileName);

            return RedirectToAction("ManageRosters");

Fiddler output:

POST /Admin/ABC.InstructorPortal/ManageRosters/UploadFile HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:3244/Admin/ABC.InstructorPortal/ManageRosters/UploadFile
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)
Content-Type: multipart/form-data; boundary=---------------------------7dc1ce2a037c
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 709
DNT: 1
Host: localhost:3244
Pragma: no-cache
Cookie: .ASPXAUTH=41A55A77A4BE37AA017D655EB941F1C41BC8349109006E8A630508A880434E5CEDF9695FDD663C87AB3379EBBC51FF5FA963B9D7E5C2294B871F152475576C72F77FD09D037C685967CC6E3FCA4725704D32148FEA2B1E1A2A838892C46E59AA88BBC02325B97565C4C07E37A8D10F636661613B152040D742F96E68D1A82C11; __RequestVerificationToken_Lw__=bWmSUvp2XNweyO7gOWfXzLIoJF0r4US7FDFj6a3JH8aBehsXhzuXBz/Ktk3E8bHNW8teZbhSRZYQCsqFhNlFza9WSE/eterDlBRdKAgRzr2voAuMimIjLtxqjjniDbxWIPllyJQOHW1MdT3OI30EN2qaSwS314vZB31xD7WlAUk=

Sep 16, 2012 at 8:25 PM

Look at how the media picker is doing it.

Sep 18, 2012 at 1:55 AM

Perfect - Works great!  It's actually in the Orchard.Media module, but was easy enough to find.