image

image

 

1.前台处理页面  DoAction  (一个参数用来传递是打开浏览(View) 还是 下载另存为命令(Download)), 第二个命令用来传递文件路径 urlf 地址

function DoAction(_command, _url)
   {
  // debugger;
   switch (_command)
   {
       case  "View" :

          window.open(‘Download.aspx?ControlClass=view&file=’+ _url,’_blank’);
          break;
       case   "Download":
           window.open(‘Download.aspx?ControlClass=download&file=’+ _url,’_self’);
           break;
       case  "Edit":
           break;
       default:
           break;

   }

}

2.后面后理页面  Donwload.aspx  & download.aspx.cs

a.Download.aspxi 源码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Download.aspx.cs" Inherits="Frameset_Download" %>
<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <ext:ScriptManager ID="ScriptManager1" runat="server">
        </ext:ScriptManager>
    <div>
    </div>
    </form>
</body>
</html>

b.download.aspx.cs源码

using System;
using System.Web;
using System.IO;
using Coolite.Ext.Web;
using Coolite.Utilities;

public partial class Frameset_Download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ControlClass = Request.Params["ControlClass"].ToString();
        String file = Request.Params["file"].ToString();
        if(ControlClass==null||ControlClass==""||file==null||file=="")
        {
            Ext.Msg.Alert("系统提示","文件错误").Show();
        }else
        {
                    OutPutFile(ControlClass,file);
            }
        }

    private void OutPutFile(string ControlClass, string filePath)
    {

        try
        {
            FileStream fs = File.OpenRead(Server.MapPath(filePath));
            BinaryReader br = new BinaryReader(fs);
            Byte[] fileData = new byte[fs.Length];
            br.Read(fileData, 0, fileData.Length);
            Response.Clear();
            Response.ClearHeaders();
            Response.BufferOutput = false;

            if (ControlClass == "view")
            {
                Response.Redirect(filePath);
            }
            else if (ControlClass == "download")
            {
                Response.ContentType = "application/force-download";
                Response.AddHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8));
                Response.AddHeader("Content-Length", fileData.Length.ToString());
                Response.BinaryWrite(fileData);
                Response.Flush();
                br.Close();
                fs.Close();
                Response.End();             
            }
            else
            {
                Ext.Msg.Alert("系统提示", "未知错误").Show();
            }
        }catch(Exception e)
        {
            Ext.Msg.Alert("文件打开错误", "可能原因:<br/>1.文件不存在                 <br/>2.权限不够<br/>3.读写错误").Show();
        }

    }

}