In an MVC project, the simplest way to typically download files is to use the location.href+ query parameter method directly.
1 varSearchparams = {2Studentname: $ ("#StudentName"). Val (),3BirthDate: $ ("#BirthDate"). Val ()4 };5 varBaseURL ="@Url. Action ("ExportData", "Student")";6Location.href = BaseURL +"?"+ $.param (searchparams);
View Code
However, if the query parameter is too long, the URL will go wrong, because the maximum limit for IE browser is 2,083 characters, if the number is exceeded, the submit button has no response. The length of the Firefox browser URL is limited to 65,536 characters, but when I test, the maximum processing time is 8,182 characters, because the length of the URL is limited by the Web server in addition to the browser restrictions. Note: Some friends may take it for granted that if the maximum length is limited to 2038 characters, it is not possible to pass about 1000 or so characters in a parameter. In fact, this is not true, for the transmission of Chinese, will eventually be urlencode after the encoding of the form of transmission, if the browser encoding is UTF8, a Chinese character after the final encoding of the length of 9 characters. (This paragraph refers to the URL maximum length issue, thank you very much)
All the landlord to use form submission, because the system has a number of pages export file function, and a page also exists to export the existence of multiple Excel, so write a plugin, what comments and suggestions welcome criticism to correct.
Original way: First define the form in advance in the interface, when the user clicks on the mentioned.
//native notation<form method="POST"action="@Url. Action ("ExportData", "Student")"Id="Exportform"> <input type="Hidden"Id="Studentname"Name="Studentname"/> <input type="Hidden"Id="birthDate"Name="birthDate"/></form>//a good way to encapsulate MVC@Html. BeginForm ("ExportData","Student", FormMethod.Post,New{id ="Exportform" }) { <input type="Hidden"Id="Stationcodes"Name="Stationcodes"/> <input type="Hidden"Id="Studentname"Name="Studentname"/> <input type="Hidden"Id="birthDate"Name="birthDate"/> }
View Code
Packaged plugins:
1 /*Export Excel2 11.13.2014-lqz-Select site URL too long ie more than 2,083 characters will be error, all URLs are integrated into form submission3 */4 ;(function () {5 varinstance;6Window. Gridexportoperate =function () {7 if(instance) {8 returninstance;9 }TenInstance = This; One This. Gridexport =function (Formpara, formId, Formurl) { A if(!$("#"+formId). length) { - varform = Document.createelement ("form"); -Form.type ="Post"; the //Form.style.display = "None"; - document.body.appendChild (form); -Form.action =Formurl; - for(iteminchFormpara) +Createinput (Form,"Hidden", item, Formpara[item]); - form.submit (); +}Else { A for(iteminchFormpara) at changeinput (item, Formpara[item]); -$("#"+formId). Submit (); - } - }; - - function Createinput (form, type, name, value) { in varTmpinput = Document.createelement ("input"); -Tmpinput.type =type; toTmpinput.name =name; +Tmpinput.value =value; - Form.appendchild (tmpinput); the } * function Changeinput (name, value) { $$('#'+name). val (value);Panax Notoginseng } - }; the}) (JQuery);
View Code
Called when a method is called to create an instance of a class.
var New gridexportoperate (); Gridxportoperate.gridexport (Formpara, FormId, BaseURL);
View Code
JS code generation form to solve the MVC URL parameter too long problem