Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roger 19 posts 57 karma points
    Mar 04, 2014 @ 18:46
    Roger
    0

    Razor File Uploads - Request.Files Always Appears Empty

    Hi All,

    I wonder if I might ask some Umbraco-y, Razor-y advice here? :) having a devil of a job getting a file upload in a form to work.  Now, I've tested a lot of code posted on these forums in the past, and none of it appeared to work - though admittedly some were a year or more old - which tells me that either something has changed about MVC in Umbraco 6.1.6 (which I am running here) or there is something subtle about the specific environment I have which is causing it to fail,  but I believe the code I have here to be sound (possibly not - the reason for posting here by way of a sanity check!)

    The form (rendered at the bottom of the Razor macro) looks like:

    <form role="form" id="some-form" action="#" method="post" enctype="multipart/form-data">

    ...

    ...

    <input type="file" id="uploadfile" name="uploadfile">

    ...

    </form>

     

    THEN! At the top of this same Macro, I'm doing:

     

    if (IsPost)

        {

     if (Request.Form["uploadfile"] != null)

                            {

                                if( Request.Files.Count > 0) {

                                var file = Request.Files[0];

                                var fileName = file.FileName;

     

      var attachment = new Attachment(file.InputStream, fileName);  

                                mail.Attachments.Add(attachment);

     

    }

    ...

    ...

     

    }

     

    The reason it appears to fail is that Request.Files is always empty - even though Request.Form["uploadfile"] appears to correctly return the (string) filename of the file uploaded.  Request.Files.Count will always return 0 and of course trying to access Request.Files[0] will alwas throw an index out of bounds-like exception.

    I don't suppose anyone has any ideas what may cause this? :) Particularly puzzling that the filename itself is available in Request.Form["<form element name>"] and yet this does not cause anything to be available in Request.Files

    Many thanks for any help or insight!

  • Roger 19 posts 57 karma points
    Mar 04, 2014 @ 19:30
    Roger
    0

    Okay well, the code (apparently!) now works fine and I can see the expected file in the Request.Files wrapper - even though I've not changed anything lol.  I've just done a diff to local with  source control and can confirm the macro is byte-for-byte identical to how it was when I posted the first message and still works lol  Glad it works, just very unsatisfying though - I need to know why it didn't work earlier or it will eat me alive! lol

    I then did a diff for all other files used by Umbraco and IIS and can confirm nothing else subtle changed anywhere outside of the usual umbraco.config, examine index files, some  txt file lists Visual Studio has placed in the /obj/ folders.

    So no worries everyone - thought I should say it's all good now, but still would be grateful and open to any thoughts on what this might have been!

Please Sign in or register to post replies

Write your reply to:

Draft