1

how to get value of added file uploaded value by using request.form in MVC Razor? i can not access "fileTypeInput" value. Look please screenshot...

        <span class="btn green fileinput-button">
            <i class="icon-plus icon-white"></i>
            <span>@Html.LocalizeGlobal("Add files...")</span>
            @* <input type="file" id="fileContainer" name="files[]" multiple />*@
            <input type="file"  name="fileTypeInput" value="Upload" />
        </span>

    </div>


   private void FileUpload(Course item)
    {
        var x = (System.Web.HttpPostedFileBase)Request.Files["uploadFile"];
        switch (Request.Form["fileTypeInput"])
        {
            case "Upload":
          . . . .
          . . . . .
        }
   }

enter image description here

4

1 回答 1

2

fileTypeInput will be inside Request.Files and not inside Request.Form. You seem to have inverted those keys:

var x = (System.Web.HttpPostedFileBase)Request.Files["fileTypeInput"];

Since your file input has the name fileTypeInput, that's the key you should use to retrieve the uploaded file from the Request.Files collection. Right now you have used Request.Files["uploadFile"] but it is unclear where this uploadFile is coming from and whether you have a file input with this name inside your form.

You could also have your controller action take a parameter with the same name:

public ActionResult SomeAction(HttpPostedFileBase fileTypeInput)
{
    ...
}

this will avoid you the need of casting and also make your action easier to unit test in isolation.

于 2013-08-06T09:08:36.923 回答