none
MVC 4 Razor File upload null RRS feed

  • Question

  • I'm trying to upload a file via MVC 4 Razor but with no success.

    I'm running on Windows 2008 R2, .NET 4.5.

    My Index.cshtml contains the following lines:

    @using (Html.BeginForm("Up", "Reply", FormMethod.Post, new { enctype = "multipart/form-data", id = "file_upload", name = "file_upload" }))

    {

    <div id="attachPane"></div>

           <input type="hidden" id="ea" name="ea" value="@ViewBag.ea" />

           <input type="hidden" id="id0" name="id0" value="@ViewBag.id0" />

           <input type="hidden" id="l" name="l" value="@ViewBag.l" />

           <input type="file" id="files" name="files" />

    }

    <script type="text/javascript">

    $(document).ready(function () {

    $('.progressbar').progressbar({ value: 0 });

    $('#file_upload').fileupload({

    dataType: 'json',

           url: '@Url.Action("Up", "Reply")',

           progressall: function (e, data) {

                    $(this).find('.progressbar').progressbar({ value: parseInt(data.loaded / data.total * 100, 10) }); },

           error: function (jqXHR, textStatus, errorThrown) { alert(textStatus + "\n" + jqXHR.responseText); },

           done: function (e, data) {

                    var control = $("#file");

                    control.replaceWith(control = control.clone(true));

                    $("#attachPane").html("");

                    $.each(data.result, function () {

                        $("#attachPane").append(

                            '<span><a title="' + this.DisplayName + '" href="' + this.OpenUrl + 'Open?DN=' + this.DA + '&Additional=' + this.Additional + '">' + this.DisplayName + '</a>&nbsp;<a href="#" onclick="da(\'' + this.DA + '\',' + '\'' + this.Additional + '\');return false;">x</a></span>'

                            );

                    });

                    $(this).find('.progressbar').progressbar({ value: 100 });

                }

            });

        });

    </script>

    The controller looks like:

    [HttpPost]

    public JsonResult Up()

    {

    List<AttLink> result = new List<AttLink>();

           foreach (string file in Request.Files)

           {

                  HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;

                  if (hpf.ContentLength == 0)

                         continue;

                  X = RenderFactory.Instance;

                  X.RendererContext.Action = "SaveAttach";

                  X.RendererContext.Id = Request["id0"];

                  X.RendererContext.Mailbox = Request["ea"];

                  X.RendererContext.AttachmentStream = hpf.InputStream;

                  X.RendererContext.SaveAttachmentFullPath = hpf.FileName;

                  X.RenderMessage();

                 result = X.RenderRemovableAttachmentListArray();

                }

               return Json(result, "application/json", JsonRequestBehavior.AllowGet);

            }

    }

    Unfortunately, when I run this code in IE it does not work. Request.Files always return NULL.

    In Firefox it works good.

    Strange think about it is that on my dev machine (when VS 2012 resides) this code works fine also for IE, it does not work after I publish the project to remote machine with the same settings as my dev machine (except of VS).

    Thank you for help

    cciapocka

    Tuesday, August 6, 2013 2:00 PM

Answers

  • Hi

    we found the solution for our problem:

    • remove JSON
    • change input "type=file" to submit
    • split sharing of value between post and get - depends on security requirements

    Hope, will help this 'workaround' to somebody too.

    cciapocka

    • Marked as answer by cciapocka Wednesday, August 7, 2013 8:33 PM
    Wednesday, August 7, 2013 8:33 PM

All replies