CodeManiac
CodeManiac

Reputation: 1014

Ajax form submission with Valums plugins in asp.net mvc 3

I have used Valums uploader plugins for file uploads in asp.net mvc 3. Following is the views which have form fields and ajax query upload button inside form. I am not sure that I am doing it right or not. What I have to change on view so that When I Choose the file to upload the form field's value is also send.

Views:

<link href="@Url.Content("~/Content/css/fileuploader.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Content/js/fileuploader.js")" type="text/javascript"></script>


@using (Html.BeginForm("Upload","AjaxUpload")) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Upload Image File</legend>
         <div class="editor-label">
           @Html.Label("Select Language")
        </div>
        <div>

          @Html.DropDownList("Language1", (SelectList) ViewBag.lang)
      </div>
         <div class="editor-label">
           @Html.Label("Select Category")
        </div>


      <div>
          @Html.DropDownList("ParentCategoryID", ViewBag.ParentCategoryID as SelectList) 
      </div>

      <div id="file-uploader">
    <noscript>
        <p>
            Please enable JavaScript to use file uploader.</p>
    </noscript>
</div>
    </fieldset>
}


**<script type="text/javascript">
    var uploader = new qq.FileUploader
    ({
        element: document.getElementById('file-uploader'),
        action: '@Url.Action("upload")', // put here a path to your page to handle uploading
        allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], // user this if you want to upload only pictures
        sizeLimit: 4000000, // max size, about 4MB
        minSizeLimit: 0 // min size
    });
</script>**

How Can I passed the value of form to controller of HTTPOST Action So that I can save data to the database. Here, I have Upload action which save the data in database but I don't know to retrieve to those value send by form post.

HttpPost Action

  [HttpPost]
        public ActionResult Upload(HttpPostedFileBase qqfile)
        {
            var wav = new PlayWav
            {
                Name = ***filename***,
                CategoryID = ***value from category dropdown select list***,
                UserID = repository.GetUserID(HttpContext.User.Identity.Name),
                LanguageID = int.Parse(***value from language dropdown select list***),
                UploadDateTime = DateTime.Now,
                ActiveDateTime = DateTime.Now,
                FilePath = "n/a"
            };



            if (qqfile != null)
            {
                // this works for IE
                var filename = Path.Combine(Server.MapPath("~/App_Data/Uploads"), Path.GetFileName(qqfile.FileName));
                qqfile.SaveAs(filename);



                return Json(new { success = true }, "text/html");
            }
            else
            {
                // this works for Firefox, Chrome
                var filename = Request["qqfile"];
                if (!string.IsNullOrEmpty(filename))
                {
                    filename = Path.Combine(Server.MapPath("~/App_Data/Uploads"), Path.GetFileName(filename));
                    using (var output = System.IO.File.Create(filename))
                    {
                        Request.InputStream.CopyTo(output);
                    }

                    **db.PlayWavs.Attach(wav);
                    db.SaveChanges();**

                    return Json(new { success = true });
                }
            }
            return Json(new { success = false });
        }

Upvotes: 0

Views: 564

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

Didn't you read the documentation? There's a whole section entitled Sending additional params. Even an example is given:

var uploader = new qq.FileUploader({
    element: document.getElementById('file-uploader'),
    action: '/server-side.upload',
    // additional data to send, name-value pairs
    params: {
        param1: 'value1',
        param2: 'value2'
    }
});

Upvotes: 1

Related Questions