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 7177587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:44:45+00:00 2026-05-28T16:44:45+00:00

Here is a controller action method that I have to upload a user’s profile

  • 0

Here is a controller action method that I have to upload a user’s profile image…

    [HttpPost]
    public ActionResult UploadPhoto(int id, FormCollection form)
    {
        Profile profile = db.Profiles.Find(id);

        var file = Request.Files[0];

        if (file.ContentLength > 512000)
        {
            ModelState.AddModelError(string.Empty, "Please limit your photo to 500 KB");
        }

        bool IsJpeg = file.ContentType == "image/jpeg";
        bool IsPng = file.ContentType == "image/png";
        bool IsGif = file.ContentType == "image/gif";

        if (!IsJpeg && !IsPng && !IsGif)
        {
            ModelState.AddModelError(string.Empty, "Only .jpeg, .gif, and .png images allowed");
        }

        if (file == null || file.ContentLength <= 0)
        {
            ModelState.AddModelError(string.Empty, "You must select an image to upload");
        }

        if (ModelState.IsValid)
        {
            try
            {
                string newFile = Path.GetFileName(file.FileName);
                file.SaveAs(Server.MapPath("/Content/users/" + User.Identity.Name + "/" + newFile));
                profile.ProfilePhotoPath = "/Content/users/" + User.Identity.Name + "/" + newFile;
                UpdateModel(profile);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        return View();
    }

When I attempt to upload an image and step through it…

When the application hits this line:

profile.ProfilePhotoPath = "/Content/users/" + User.Identity.Name + "/" + newFile;

It shows the ProfilePhotoPath value to be “System.Web.HttpPostedFileWrapper”

Now when the application hits the next line:

UpdateModel(profile);

It shows the ProfilePhotoPath value to be “/Content/users/WebWired/myprofilepic.png” , as it should…

But then, when the application hits the next line:

db.SaveChanges();

All of a sudden the ProfilePhotoPath value is “System.Web.HttpPostedFileWrapper” again… and that is how it is saved…

If that isn’t odd enough, it did work before I started adding logic to the file upload, but that really shouldn’t have anything to do with it because it passes all of that up…

Does anyone understand what is going on here, why is it doing this, am I doing something wrong?

  • 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-28T16:44:46+00:00Added an answer on May 28, 2026 at 4:44 pm

    UpdateModel() updates your profile object with values from the controller’s value provider – i.e., POST parameters etc. If it finds a POST parameter called “ProfilePhotoPath”, your profile.ProfilePhotoPath property will be set to that value, overwriting the value you’ve just set manually.

    Your <input type="file"> field (or whatever method you use to post the file to the server) seems to have a name attribute: “ProfilePhotoPath”. That field will be turned into a HttpPostedFileWrapper object on the server, containing the information about the posted file (content length, type, filename etc.). And it’s that object UpdateModel will assign to your profile.ProfilePhotoPath property (because it has the same name as the property). Since it’s assigning an object to a string property, it’ll coerce the object into a string, yielding “System.Web.HttpPostedFileWrapper”.

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

Sidebar

Related Questions

On ASP.NET MVC 3, assume that we have following controller action: public ActionResult Index()
Here is the idea: When the user wants to see /controller/action, then I want
I'm struggling here with a problem: I have a controller questions which has action
I have an Action Method that returns a JSON-serialized object. The problem I'm having
i wish to have a simple Action in my controller that accepts a few
Here is the set-up: I have a Notifications controller that is called from task
Let's assume that we have a controller with some action which could look like:
Here is the first part of my controller code: public class ControlMController : Controller
Here is the thing. I currently have a tabBar controller, with several navigation controllers
So I have a simple bit of code that re-sizes my profile images as

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.