Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jquery、通用处理程序和等待状态下载文件_C#_Jquery_Asp.net - Fatal编程技术网

C# 使用jquery、通用处理程序和等待状态下载文件

C# 使用jquery、通用处理程序和等待状态下载文件,c#,jquery,asp.net,C#,Jquery,Asp.net,环境:.NET3.5,jQuery 2.1.4 结果Hello,World会在回调中返回,但是有没有办法让它作为附件返回 jquery: function test() { $.ajax({ type: "POST", url: "Handler1.ashx", contentType: "application/json; charset=utf-8", success: function (data) {

环境:.NET3.5,jQuery 2.1.4

结果
Hello,World
会在回调中返回,但是有没有办法让它作为附件返回

jquery:

function test() {
    $.ajax({
        type: "POST",
        url: "Handler1.ashx",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $('#progressBar').hide();
            //alert(data);                
        }
    });
public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //Create and populate a memorystream with the contents of the 
            //database table 
            System.IO.MemoryStream mstream = GetData();
            //Convert the memorystream to an array of bytes. 
            byte[] byteArray = mstream.ToArray();
            //Clean up the memory stream 
            mstream.Flush();
            mstream.Close();
            // Clear all content output from the buffer stream 
            context.Response.Clear();
            // Add a HTTP header to the output stream that specifies the default filename 
            // for the browser's download dialog 
            context.Response.AddHeader("Content-Disposition", "attachment; filename=mytextfile.txt");
            // Add a HTTP header to the output stream that contains the 
            // content length(File Size). This lets the browser know how much data is being transfered 
            context.Response.AddHeader("Content-Length", byteArray.Length.ToString());
            // Set the HTTP MIME type of the output stream 
            context.Response.ContentType = "application/octet-stream";
            // Write the data out to the client. 
            context.Response.BinaryWrite(byteArray);
        }

        private MemoryStream GetData()
        {
            //Create the return memorystream object that will hold 
            //the buffered data. 
            MemoryStream ReturnStream = new MemoryStream();
            try
            {
                Thread.Sleep(5000); //Simulate some work.

                StreamWriter sw = new StreamWriter(ReturnStream);

                //Write the row of data to the Memory Stream. 
                sw.WriteLine("Hello, World");

                //Clean up the stream writer 
                sw.Flush();
                sw.Close();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            //Return the memory Stream 
            return ReturnStream;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
通用处理程序:

function test() {
    $.ajax({
        type: "POST",
        url: "Handler1.ashx",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $('#progressBar').hide();
            //alert(data);                
        }
    });
public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //Create and populate a memorystream with the contents of the 
            //database table 
            System.IO.MemoryStream mstream = GetData();
            //Convert the memorystream to an array of bytes. 
            byte[] byteArray = mstream.ToArray();
            //Clean up the memory stream 
            mstream.Flush();
            mstream.Close();
            // Clear all content output from the buffer stream 
            context.Response.Clear();
            // Add a HTTP header to the output stream that specifies the default filename 
            // for the browser's download dialog 
            context.Response.AddHeader("Content-Disposition", "attachment; filename=mytextfile.txt");
            // Add a HTTP header to the output stream that contains the 
            // content length(File Size). This lets the browser know how much data is being transfered 
            context.Response.AddHeader("Content-Length", byteArray.Length.ToString());
            // Set the HTTP MIME type of the output stream 
            context.Response.ContentType = "application/octet-stream";
            // Write the data out to the client. 
            context.Response.BinaryWrite(byteArray);
        }

        private MemoryStream GetData()
        {
            //Create the return memorystream object that will hold 
            //the buffered data. 
            MemoryStream ReturnStream = new MemoryStream();
            try
            {
                Thread.Sleep(5000); //Simulate some work.

                StreamWriter sw = new StreamWriter(ReturnStream);

                //Write the row of data to the Memory Stream. 
                sw.WriteLine("Hello, World");

                //Clean up the stream writer 
                sw.Flush();
                sw.Close();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            //Return the memory Stream 
            return ReturnStream;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
资源:

jQuery ajax无法正确处理二进制响应(无法设置 responseType),因此最好使用普通的XMLHttpRequest调用


它必须是一篇ajax文章吗?如果GET还可以,您可以尝试不使用ajax,如下所述:很抱歉耽搁了您的时间,现在正在研究这种可能性……不要忘记,仍在测试……brbIs它很有用吗@在本例中,最后一行代码引用“frm”。什么是“frm”及其定义在哪里?