0

I have a form with 3 inputs (text, image, submit button).

@using (Html.BeginForm("Save", "User", FormMethod.Post, new {Id="Form1", enctype = "multipart/form-data"}))
        {
        <input id="FileUploadInput" name="Image" type="file"/>
        <input id="FirstName"  Name="FirstName">
        <input type="submit" id="inputSubmit" value="Save" />
        }

Now i want to submit this form from javascript with AJAX

$("#inputSubmit").click(function (e) {
            e.preventDefault();
            var form = $("#Form1");
            form.validate();
            if (form.valid()) {
                $.ajax({
                    url: "/User/Save",
                    data: form.serialize(),
                    type: "POST",
                    success: function (data) {
                        if (data === "") {
                            location.reload();
                        }
                        else {
                            $(".content").html(data);
                            $.validator.unobtrusive.parse($(".content"));
                        }
                    }
                });
            }

            return false;
        });

In my controller file i have.

public ActionResult Save(UserProfileSettings settings)
{
  var image = setings.Image
  var name = settings.Firstname
}

My model

public class UserProfileSettings
{
   public string FirstName { get; set; }
   public HttpPostedFileBase Image { get; set; }
}

The problem is that in my controller method i am getting settins.FirstName, but settings.Image is always null. I think, that with this method it is not possible to serialize image file.

1

2 Answers 2

0

try use jquery plugin muliple upload: http://blueimp.github.io/jQuery-File-Upload/

Sign up to request clarification or add additional context in comments.

Comments

0

As Darin Dimitrov suggested before, it's better to use jquery forms plugin. I have already posted this in my another answer here.

Quick Example

View

@using (Ajax.BeginForm("YourAction", "YourController", new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data"}))
{
    @Html.AntiForgeryToken()
    <input type="file" name="files"><br>
    <input type="submit" value="Upload File to Server">
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public void YourAction(IEnumerable<HttpPostedFileBase> files)
{
    if (files != null)
    {
        foreach (var file in files)
        {
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // TODO: need to define destination
                var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
                file.SaveAs(path);
            }
        }
    }
}

4 Comments

But i want to submit the form from javascript method $("#inputSubmit").click(function (e) {
@ArmenMkrtchyan: Ajax.BeginForm() is kind of jquery ajax form submit which makes so easy to do this. stackoverflow.com/questions/5410055/…
Down voter should see Darin Dimitrov suggestion too. stackoverflow.com/questions/2780241/…
Even see this example too stackoverflow.com/questions/581703/…

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.