2013-04-25 95 views
0

在下面的代碼中,首先允許用戶從下拉列表中選擇選項並允許瀏覽文件。 代碼:發送表單數據xmlhttprequest

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); 

} 在CH下拉選項會保存。 我想CH值發送到控制器 控制器代碼:

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

我怎樣才能把它?

回答

1

你可以把它添加到FormData

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

和你的控制器動作,現在可能看起來像這樣:

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