pihu
pihu

Reputation: 59

sending form data xmlhttprequest

In below code first am allowing the user to select the option from the drop down list and than allows to browse the file. Code:

  function choice() {
   var box = dhtmlx.modalbox({
       text: "<div id='form_in_box'><div>Choose a File to Convert <hr/><label>Filename: <input type='file' name='file' id='file' style='width: 400px; height: 27px;'></label><br></div><div><span class='dhtmlx_button'><input type='submit' value='Create PDF' style='width: 86px' onclick='Convert(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>",
       width: "300px"
   });

}

 function Convert(box) {
   var ch = document.getElementById('choice');
   var file = document.getElementById('file');
   if (file.value == "") {
       alert("Choose a file to convert");
       return false;
   }

   dhtmlx.modalbox.hide(box);
   var fd = new FormData();
   fd.append('file', file.files[0]);
   var xhr = new XMLHttpRequest();
   xhr.open('POST', '/FileUpload/Convert', true);
   xhr.onreadystatechange = function () {
       if (xhr.readyState == 4 && xhr.status == 200) {
           alert('File successfully conveted to PDF');
       }
   };
   xhr.send(fd);

} In ch the drop down option gets saved. I want to send ch value to the controller Controller code:

public ActionResult Convert(HttpPostedFileBase file, FormCollection data)
    {
        string choice = data["choice"];

how can i send it?

Upvotes: 0

Views: 1679

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

You could add it to the FormData:

var fd = new FormData();
fd.append('file', file.files[0]);
fd.append('choice', ch.value);

and your controller action may now look like this:

public ActionResult Convert(HttpPostedFileBase file, string choice)
{
    ...
}

Upvotes: 2

Related Questions