Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

c# - MVC HttpPostedFileBase always null

I have this controller and what I am trying to do is to send an image to the controller as a [byte], this is my controller:

[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image) 
 {

            if (image != null) 
            {
                product.ImageMimeType = image.ContentType;
                product.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(product.ImageData, 0, image.ContentLength);
            }

            _db.Products.Add(product);
            _db.SaveChanges();

            return View();
 }

and on my view:

@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) {

    <fieldset>
        <legend>Product</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Category)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Category)
            @Html.ValidationMessageFor(model => model.Category)
        </div>

        <div>
        <div>IMAGE</div>
        <input type="file" name="image" />

        </div>


        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

but the problem is that on my controller the value for image is alway null, I cant seem to get any information on the HttpPostedFileBase

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add the encType with multipart/form-data.

@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post, new {enctype = "multipart/form-data" })) {

You can always add it to your model too as follows, providing it is a ViewModel:

public class Product 
{
    public Product()
    {
        Files = new List<HttpPostedFileBase>();
    }

    public List<HttpPostedFileBase> Files { get; set; }
    // Rest of model details
}

You can the retrieve the files by removing the un-needed parameter i.e.

[HttpPost]
public ActionResult AddEquipment(Product product) 
 {
    var file = model.Files[0];
    ...
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...