0

I have problem with uploading my file. I want to upload it from my edit view:

    <% 
    using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" }))
    {%>
    <%: Html.ValidationSummary(true) %>

    <%: ViewData["ErrorMessage"] %>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Image) %>
        </div>
        <div class="editor-field">
            <input type="file" id="Image" name="Image" />
            <label id="LabelErrorImage" class="errorMessage" />
        </div> 

        <p>
            <input type="submit" value="Save" onclick="return Validate(); return false;"/>
        </p>
    </fieldset>

<% } %>

I want to use HttpPostedFileBase class. My edit action:

[Authorize]
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, HttpPostedFileBase file, FormCollection formValues)
        {
                    if (ModelState.IsValid)
                    {

                        if (file != null && file.ContentLength > 0)
                        {
                            CustomHelpers.createFolder();
                            var tmpPath = MyConfig.UPLOAD_FILE_PATH + "/" + Membership.GetUser().ProviderUserKey.ToString();
                            var path = Path.Combine(Server.MapPath(MyConfig.UPLOAD_FILE_PATH), "Avatar");
                            var fileExtension = Path.GetExtension(file.FileName);
                            file.SaveAs(path);
                            user.Image = "Avatar";
                        }
                        adventureDB.SaveChanges();

                        return RedirectToAction("Index");
                    }
        }

But I always have empty the file object, why????? Do you have any ideas, suggestions why it can work like that? Maybe there is problem how I pass on the file value to my Edit action?


EDIT: IT IS REALLY STRANGE AS EVEN WHEN I REMOVE
using (Html.BeginForm("Index","Profile",FormMethod.Get, new { enctype="multipart/form-data" }))

The page source still has:

<body>

    <form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1">

I mean I can not change the tag but have no idea why :/

3 Answers 3

1

Try changing:-

public ActionResult Edit(string id, HttpPostedFileBase file, 
  FormCollection formValues)

to:-

public ActionResult Edit(string id, HttpPostedFileBase image, 
  FormCollection formValues)

as the name of your input is image

 <input type="file" id="Image" name="Image" />

edit To be honest something else is stopping the binding of image. Is this the whole form that you have posted?

A few things to test

  1. You have HTTPOST decorating your method twice, although I don't believe this should make a difference.
  2. View the source and make sure there is nothing else named name=image in the source.
  3. Make sure you empty your cache and make sure source is correct before testing again
  4. Try using <form action="/profile/index" method="post" enctype="multipart/form-data">
  5. Judging by your last edit you have a problem with master pages/layout? Is this a mvc/webforms hybrid?
Sign up to request clarification or add additional context in comments.

5 Comments

Yes true, I have changed it but it still has null :/
No it is not all code as it is very long but I noticed that it does not matter what I write in my view as always it is redirecting to the same function. Maybe because I use Master site and it uses a mechanism which I do not know?
Further more I have 2 forms in the source code: <form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMjIyODQ4MTE2ZGQ0zXwQxrpoQd+by+EOC4qY3mQAo7zW2FgvbThGQUWMVQ==" /></div> and then the second which I wrote in view: <form action="/Profile/Edit" enctype="multipart/form-data" method="POST">
Yes I have found the mistake!!!! In Master.Site it default creates form and there can not be nested forms as the second one is ignored! I have lost so many time to found it out! Thanks for help!
No worries, perhaps you could mark this question as answered, thanks
1

The solution of this problem when:

  1. We use Master.Site,
  2. We want to upload file in a view,
  3. We are sure that it should work but we all the time has null,

Then:

  1. Guys were right - I had wrong name in my view - check it!
  2. Check source code of your view and if you have 2 < form > tags you should remove the < form > tag from Master site as then the second one is ignored!

Now it should work.

Comments

0

Well, in your view you named the file input 'image' but your action method accepts a parameter called 'file'. Rename one of those and it should work.

1 Comment

Yes, but I have changed it now and still the same :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.