0

When I try to upload a file, it is always null but the username passes over.

<div class="container">
    @using(Html.BeginForm("Upload", "ADImage", FormMethod.Post, new { enctype = "mulipart/form-data" }))
    { 
        <fieldset>
            <legend>AD Image Upload</legend>
            <label>AD Username</label>
            <div class="input-control text" data-role="input-control">
                <input type="text" name="username" placeholder="Username" />
                <button class="btn-clear" tabindex="-1" type="button"></button>
            </div>
            <label>Choose File</label>
            <div class="input-control file info-state" data-role="input-control">
                <input type="file" name="file" tabindex="-1" style="z-index: 0;" />
                <input type="text" id="input_file_wrapper" readonly style="z-index: 1; cursor: default;" />
                <button class="btn-file" type="button"></button>
            </div>

            <button type="submit" value="Submit">Submit</button>
        </fieldset>
    }
</div>

Controller

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file, string username)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            ViewBag.IsUserValid = false;
        }
        else if (file == null | file.ContentLength.Equals(0))
        {
            ViewBag.IsFileValid = false;
        }
        else
        {
            byte[] fileBytes = ReadFile(file.InputStream);
        }
        return RedirectToAction("Index");
    }
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • if the file is `null` then you should end up with Null Reference exception based on your current check `if (file == null | file.ContentLength.Equals(0))` – Habib Apr 30 '15 at 15:46
  • 4
    You got a typo in the `enctype`, should be `"multipart/form-data"` - t is missing – Andrei Apr 30 '15 at 15:47
  • @Habib that's in the `else`, so it's probably not being hit since `username` is being passed in. But that's definitely a good point - should be `||` instead of `|`. – Joe Enos Apr 30 '15 at 15:48
  • If file is null, the file.ContentLength.Equals(0) expression will not be evaluated because the whole expression is already true. No NullReferenceException. – Christoph Apr 30 '15 at 15:51
  • I guess you have JavaScript to select the file and show it in the input? can you show it to us? – Gabriel GM Apr 30 '15 at 15:51
  • @Andrei That was the issue – Tsukasa Apr 30 '15 at 15:58
  • @Andrei: Please post your comment as an answer so the OP can accept it. – Chris Pratt Apr 30 '15 at 15:58
  • 1
    @christoph Your comment is true for the double-pipe (`||`), which is the usual one you see in C#. However, the code here has a single-pipe (`|`), which is still valid, but does not result in short-circuiting, which means both sides of the pipe will execute. That's why the double-pipe is much more common. – Joe Enos Apr 30 '15 at 16:47
  • 1
    Related to @JoeEnos comment: http://stackoverflow.com/questions/35301/what-is-the-difference-between-the-and-or-operators Don't use bitwise operators when you mean to use logical operators. – Eric King Apr 30 '15 at 18:00

1 Answers1

4

There is a typo in your enctype field of the form. It is

"mulipart/form-data"

but should be

"multipart/form-data"

Note the missing "t".

Andrei
  • 55,890
  • 9
  • 87
  • 108