.net 2.0和.net 3.5分别下载一个文件

一、.net 2.0
protected void btn_Download_OnClick(object sender, EventArgs e)
{
    string templatePath = HttpContext.Current.Server.MapPath("~/Template");
    string downloadFilePath = Path.Combine(templatePath, "UpdateSalePrice.xls");
    try
    {
        string filePath = downloadFilePath;
        if (File.Exists(filePath))
        {
            FileInfo info = new FileInfo(filePath);
            long fileSize = info.Length;
            HttpContext.Current.Response.Clear();

            //指定Http Mime格式为压缩包
            HttpContext.Current.Response.ContentType = "application/x-zip-compressed";

            // Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:
            // Content-Disposition: attachment;filename=filename.txt
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=UpdateProductPrice.xls");
            //不指明Content-Length用Flush的话不会显示下载进度   
            HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
            HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);
            HttpContext.Current.Response.Flush();
        }
    }
    catch (Exception ex)
    {
        this.lbl_Info.Text = ex.Message;
    }
    finally
    {
        HttpContext.Current.Response.Close();
    }
}


二、.net 3.5

//下载
protected void btnDownload_OnClick(object sender, EventArgs e)
{
    string templatePath = HttpContext.Current.Server.MapPath("~/Template/ImportExcel");
    string downloadFilePath = Path.Combine(templatePath, "AAAAA.xls");
    this.Context.ResponseApplicationFile(downloadFilePath, "BBBBB", false);
}

public static void ResponseApplicationFile(this HttpContext context, string filePath, string saveAsName, bool isDirectOpen)
{
    HttpResponse Response = context.Response;
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = MimeUtility.GetMimeType(filePath);
    string downloadFileSaveAsName = string.Empty;
    if (string.IsNullOrEmpty(System.IO.Path.GetFileNameWithoutExtension(saveAsName)) || string.IsNullOrEmpty(saveAsName))
    {
        downloadFileSaveAsName = System.IO.Path.GetFileName(filePath);
    }
    else if (System.IO.Path.GetExtension(filePath).ToUpper() == System.IO.Path.GetExtension(saveAsName))
    {
        downloadFileSaveAsName = saveAsName;
    }
    else
    {
        downloadFileSaveAsName = saveAsName + System.IO.Path.GetExtension(filePath);
    }
    Response.HeaderEncoding = Encoding.UTF8;
    if (System.IO.File.Exists(filePath))
    {
        Response.AddHeader("content-disposition", (isDirectOpen ? "" : "attachment;") + " filename=" + HttpUtility.UrlEncode(downloadFileSaveAsName, Encoding.UTF8) + ";size=" + (new FileInfo(filePath)).Length.ToString());
        byte[] contents = System.IO.File.ReadAllBytes(filePath);
        Response.BinaryWrite(contents);
    }
    else
    {
        Response.AddHeader("content-disposition", (isDirectOpen ? "" : "attachment;") + "filename=" + HttpUtility.UrlEncode(downloadFileSaveAsName, Encoding.UTF8) + ";size=0");
    }
    Response.Flush();
    Response.End();
}


 

你可能感兴趣的:(.net 2.0和.net 3.5分别下载一个文件)