Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:14:57+00:00 2026-05-31T13:14:57+00:00

I am trying to allow a user to upload an image to our website

  • 0

I am trying to allow a user to upload an image to our website and I’m not quite sure about how to use this. I have tried to use multiple types to define the image, including System.Drawing.Image and HttpPostedFileWrapper but the @Html.EditorFor always (understandably) brings up its attributes as fields to edit.

In my view I did have, instead of @Html.EditorFor I did have <input type="file" name="imageToUpload" /> but it didn’t get taken through to my view as part of the Model? I am quite new to MVC so I am hoping it is something trivial.

Here is my View:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>New Image</legend>

        <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.Image)
        </div>
        <div class="editor-field">
            <input type="file" name="imageToUpload" />
        </div>

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

My Controller:

    [HttpPost]
    public ActionResult CreateImage(string brand, string collection, ImageEditViewModel imageEditViewModel)
    {
        string fileName = Guid.NewGuid().ToString();
        string serverPath = Server.MapPath("~");
        string imagesPath = serverPath + String.Format("Content\\{0}\\Images\\", Helper.Helper.ResolveBrand());

        string newLocation = Helper.Helper.SaveImage(fileName, imagesPath, imageEditViewModel.Image.InputStream)

        Image image = new Image
        {
            Collection = ds.Single<Collection>(c => c.Season == collection
                && c.Brand.Name == brand),
            Description = imageEditViewModel.Description,
            Location = "newLocation",
            Order = Helper.Helper.GetImageOrder(brand, collection)
        };

        ds.InsertOnSubmit<Image>(image);
        ds.SubmitChanges();

        return RedirectToAction("Brand");
    }

And finally the ViewModel:

public class ImageEditViewModel
{
    public int CollectionId { get; set; }

    public string Description { get; set; }

    public HttpPostedFileWrapper Image { get; set; }

    public int Order { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:14:58+00:00Added an answer on May 31, 2026 at 1:14 pm

    Ensure to specify the correct enctype="multipart/form-data" on your form or you won’t be able to upload files:

    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>New Image</legend>
    
            <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.ImageToUpload)
            </div>
            <div class="editor-field">
                <input type="file" name="imageToUpload" />
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    

    And if you wanted to use an EditorFor helper to generate the file input you could use the following:

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

    and then define a custom editor template for the HttpPostedFileBase type (see below that you need to modify your model to use this type actually). So the editor template in ~/Views/Shared/EditorTemplates/HttpPostedFileBase.cshtml:

    @model HttpPostedFileBase
    @Html.TextBox("", null, new { type = "file" })
    

    and on your view model use the HttpPostedFileBase type and make sure that the name of the property matches the name of the file input on your form:

    public class ImageEditViewModel
    {
        public int CollectionId { get; set; }
    
        public string Description { get; set; }
    
        public HttpPostedFileBase ImageToUpload { get; set; }
    
        public int Order { get; set; }
    }
    

    Also make sure to checkout the following blog post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a website that will allow user to upload video files
I'm trying to configure CKEditor to allow the user to upload an image from
Similar to Twitter, I am trying to allow a user to upload their own
I am trying to allow the user to upload as many images as they
I'm trying to use the following to allow a user to drag photos onto
i'm trying to allow user to upload multiple images but when i validate the
I'm having a user upload a picture, I'm trying to allow them to stay
I'm trying to do a feature that would allow the user to upload a
So I'm trying to add some ability to my project to allow user-defined properties
I trying to pomp showPermissionDialog for allow the user to post something in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.