jquery's Ajax functions return type only XML, text, JSON, HTML and other types, there is no "stream" type, so we want to implement Ajax download, can not use the corresponding Ajax functions for file download. But you can use JS to generate a form, use this form to submit parameters, and return the "stream" type of data. The page is not refreshed during the implementation process.
<th><inputtype= "checkbox"name= "title"ID= "Checkbox_all"/></th>{% for student in students%}<TR> <TD><inputtype= "checkbox"name= "Student_name"ID= "boxes"value= "{{student.number}}"/></TD>
$ (' #checkbox_all '). Click (function () { varA = document.getElementsByTagName ("Input"); if(document.getElementById ("Checkbox_all"). Checked = =true) { for(vari = 0; i < a.length; i++) if(A[i].type = = "checkbox") a[i].checked =true; } Else { for(vari = 0; i < a.length; i++) if(A[i].type = = "checkbox") a[i].checked =false; } } );
$ (' #input_student '). Click (function () { vararr =NewArray (); varSign = 0; varInputs = document.getElementsByTagName (' input ');//gets all the input tag objects. for(vari = 0; i < inputs.length; i++) { varobj =Inputs[i]; if(Obj.type = = ' checkbox ') { if(obj.checked = =true) { sign= 1; varLength =Arr.push (Obj.value); } } } if(sign = = 0)//No selected items{alert ("Please tick the data you want to download!" "); return; } if(!confirm ("OK to download selected entries?") "))return; varForm = $ ("<form>"); Form.attr (' Style ', ' Display:none '); Form.attr (' Target ', '); Form.attr (' Method ', ' post '); Form.attr (' Action ', ' {% url ' teacher:downloadstu '%} '); varINPUT1 = $ (' <input> ')); Input1.attr (' type ', ' hidden '); Input1.attr (' Name ', ' number '); Input1.attr (' Value ', Arr.join ("&")); $(' Body '). append (form); Form.append (INPUT1); Form.submit (); Form.remove (); });
If you submit more than one data, use a few more input.
Ajax generation stream file download and check box implementation