Ajax实现下载文件功能

刚开始学习Asp的时候我们实现下载功能可能是这样
    ASP:
protected void btn2_onclick(object sender, EventArgs e)
{
    string fileName = "test.txt";
    string filePath = HttpContext.Current.Server.MapPath("~/" + fileName);
    if (File.Exists(filePath))
    {
        Response.ContentType = "application/unknow";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
        Response.TransmitFile(filePath);
    }
}


后来想用Ajax,于是开始动手做

    Ajax:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1
{
    /// 
    /// HandlerDownFile 的摘要说明
    /// 
    public class HandlerDownFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string fileName = context.Request["fileName"];
                string filePath = context.Server.MapPath("~/" + fileName);
                if (File.Exists(filePath))
                {
                    context.Response.ContentType = "application/unknow";
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
                    context.Response.TransmitFile(filePath);
                }
            }
            catch (IOException ex)
            {    
                throw new Exception(ex.Message,ex);
            }
            finally
            {
                context.Response.Close();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


然后发现问题来了,我返回的数据流前端没接头人,那怎么保存?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownFile.aspx.cs" Inherits="WebApplication1.login" %>






下载文件
    
    


    
Ajax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1
{
    /// 
    /// HandlerDownFile 的摘要说明
    /// 
    public class HandlerDownFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string fileName = context.Request["fileName"];
                string filePath = context.Server.MapPath("~/" + fileName);
                if (File.Exists(filePath))
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    byte[] bytes = new byte[(int)fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
                    context.Response.BinaryWrite(bytes);
                    context.Response.Flush();
                    context.Response.End();
                }
            }
            catch (IOException ex)
            {    
                throw new Exception(ex.Message,ex);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

你可能感兴趣的:(C#)