c# - File Download in javascript using AJAX call -
this java script code downloading file database on button click, when button clicks function calls. using ajax call have moved handler.
function downloaddocument() { var currentuserempid = currentselecteduser; ext.ajax.request({ url: "uploadattachment.ashx?mode=downloaddocument&employeeid=" + currentuserempid, success: function (response) { var data = response.responsetext; }, failure: function (form, action) { } }); }
here comes handler page, have got bytes of file byte[] buffer. problem here download not working. could't figure out problem, since iam beginner. please this, thankyou.
case "downloaddocument": webclient web = new webclient(); try { byte[] buffer; var query2 = @"select lld_decleration_doc (select instance, employee_id, lld_decleration_doc, row_number() over(partition employee_id order update_date desc) latest [emanager].[dbo].[tax_benefit_declaration]) latest = 1 , employee_id = @employeeid"; using (sqlconnection con = new sqlconnection(db.connectionstring)) using (sqlcommand cmd = new sqlcommand(query2, con)) { sqlparameter param = cmd.parameters.add("@employeeid", sqldbtype.int); param.value = employeeid; con.open(); buffer = (byte[])cmd.executescalar(); con.close(); } httpresponse response = httpcontext.current.response; response.clear(); response.clearcontent(); response.clearheaders(); response.buffer = true; response.contenttype = "application/octet-stream"; string header = "attachment; filename=newfile"; response.appendheader("content-disposition", header); context.response.binarywrite(buffer); response.end(); } catch { } break; }
this said many times. cant ajax call.
you can achive invoke hidden iframe
example:
var body = ext.getbody(); var comp = body.getbyid('hiddenform-iframe-download'); if (!ext.isempty(comp)) { comp.remove(); } body.createchild({ tag: 'iframe', cls: 'x-hidden', id: 'hiddenform-iframe-download', name: 'iframe', src: "yourcontexttodownload?param1="+something });
Comments
Post a Comment