使用AJAX调用在javascript中下载文件 [英] File Download in javascript using AJAX call

查看:124
本文介绍了使用AJAX调用在javascript中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的java脚本代码,用于从按钮点击的数据库中下载文件,当按钮点击此功能调用。使用ajax调用我已经移动到处理程序。

This is my java script code for downloading the file from database on button click, when the button clicks this function calls. using ajax call i have moved to 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) {
        }
    });
}

这里是处理程序页面,我有文件的字节为 byte [] buffer 。这里的问题是下载不起作用。我不知道这个问题,因为Iam是初学者。请帮助,谢谢。

Here comes the handler page, I have got bytes of my file to byte[] buffer. The problem here is download not working. I could't figure out the problem, since Iam a beginner. Please help with this, Thankyou.

case "DownloadDocument":

            WebClient web = new WebClient();
            try
            {
                byte[] buffer;

                var query2 = @"select LLD_Decleration_doc from (select  instance, Employee_id, lld_Decleration_doc, ROW_NUMBER() OVER(PARTITION BY Employee_id ORDER BY Update_Date DESC) Latest from [EManager].[dbo].[tax_benefit_declaration]) a where latest = 1 And 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;
    }


推荐答案

多次说您可以通过Ajax调用来执行此操作。

This is something that it was said many times. You cant do this with Ajax call.

您可以通过调用隐藏的 iframe 来实现此目的:

You can achive this by invoke hidden iframe for 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
                });

这篇关于使用AJAX调用在javascript中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆