文件通用上传下载

来源:互联网 发布:游戏破解器软件 编辑:程序博客网 时间:2024/05/22 13:09

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoadFile.aspx.cs" Inherits="Default" %>

<!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">
        <div style="text-align: center">
            <table border="0" cellpadding="0" cellspacing="0" width="1002" height="573" id="table1">
                <tr>
                    <td>
                    <img src="images/index1_01.jpg" id="top" alt="" width="1002" height="108" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 1002px" background="images/index1_02.jpg" height="370">
                        <div style="text-align: center">
                            <table border="0" cellpadding="0" cellspacing="0" style="width: 450px" height="130">
                                <tr>
                                    <td style="height: 10px">
                                        <asp:Label ID="Label1" runat="server" Width="300px" ForeColor="Red" Height="15px"></asp:Label></td>
                                </tr>
                                <tr>
                                    <td height="100">
                                        <asp:Panel ID="Panel1" runat="server" Height="90px" ScrollBars="Auto" Width="300px">
                                          <table id="F" runat="server" cellpadding="0" cellspacing="0" enableviewstate="true">
                                            <tr>
                                              <td style="width: 100px; height: 40px">
                                              <asp:FileUpload ID="FileUpload1" runat="server" />
                                              </td>
                                            </tr>
                                          </table>
                                        </asp:Panel>
                                    </td>
                                </tr>
                                <tr>
                                    <td height="10">
                                        </td>
                                </tr>
                                <tr>
                                    <td height="10">
                                        <asp:ImageButton ID="ImageButtonUp" runat="server" ImageUrl="~/images/shanchuan.gif" OnClick="ImageButtonUp_Click" />
                                        <asp:ImageButton ID="ImageButtonDown" runat="server" ImageUrl="~/images/xiazai.gif" PostBackUrl="~/downFile.aspx" />
                                        </td>
                                </tr>
                            </table>
                        </div>
                   
                    </td>
                </tr>
                <tr>
                    <td style="width: 1002px">
                    <img src="images/index1_03.jpg" alt="" width="1002" height="95" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>
=========================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    private void UpFile()
    {
        string FilePath=Server.MapPath("./")+"File";
        HttpFileCollection HFC = Request.Files;
        for (int i = 0; i < HFC.Count; i++)
        {
            HttpPostedFile HF = HFC[i];
            try
            {
                if (HFC[i].ContentLength > 0)
                {

                    HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName));
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"alert", "<script>alert('上传成功!')</script>");
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('对不起,请先选择上传的文件路径!')</script>");
                }
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('对不起,下载失败!')</script>");
            }
        }
    }
    protected void ImageButtonUp_Click(object sender, ImageClickEventArgs e)
    {
        UpFile();
    }
}
=================================以上是上传页面cs和aspx代码==============================

=================================以下是下载页面cs和aspx代码==============================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="downFile.aspx.cs" Inherits="downFile" %>

<!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">
    <div>
    <table id="__01" width="1002" height="573" border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td>
   <img src="images/index1_01.jpg" width="1002" height="108" alt=""/></td>
 </tr>
 <tr>
  <td height="370" background="images/index1_02.jpg"><table width="461" height="184" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="358" height="38">&nbsp;<asp:Label ID="Label1" runat="server" Font-Size="9pt"></asp:Label></td>
            <td width="101">
                <asp:ImageButton ID="ImageButtonUp" runat="server" ImageUrl="images/shanchuan.gif"
                    PostBackUrl="LoadFile.aspx" /><a href="#"></a></td>
          </tr>
          <tr>
            <td colspan="2" valign="middle"><table width="437" height="118" align="center" cellpadding="0" cellspacing="0">
                <tr>
                  <td align="center" valign="bottom">&nbsp;</td>
                  <td align="left" valign="middle">&nbsp;
                                <asp:ListBox ID="ListBox1" runat="server" Height="116px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
                                    Width="212px"></asp:ListBox></td>
                </tr>
               
                <tr>
                  <td width="68" height="50">&nbsp;</td>
                  <td width="367">
                      <asp:ImageButton ID="ImageButtonDown" runat="server" ImageUrl="images/xiazai.jpg" OnClick="ImageButton1_Click" /><a href="#"></a></td>
                </tr>

            </table></td>
          </tr>
        </table></td>
 </tr>
 <tr>
  <td>
   <img src="images/index1_03.jpg" width="1002" height="95" alt="" /></td>
 </tr>
</table>
   
    </div>
    </form>
</body>
</html>
===================================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class downFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)//首次加载
        {
            addListBox();              
        }
    }
    protected void addListBox()
    {
        //将指定文件夹中的文件保存到字符串数组中
        string[] name = Directory.GetFiles(Server.MapPath("File"));
        foreach (string s in name)
        {
            //将文件名添加到ListBox中
            ListBox1.Items.Add(Path.GetFileName(s));
        }    
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["txt"] = ListBox1.SelectedValue.ToString();
    }

 
    protected void dFile()
    {
        //判断是否选择文件名
        if (ListBox1.SelectedValue != "")
        {
            if (Session["txt"] != "")
            {   //获取文件路径
                string path = Server.MapPath("File/") + Session["txt"].ToString();
                //初始化 FileInfo 类的实例,它作为文件路径的包装
                FileInfo fi = new FileInfo(path);
               
                //判断文件是否存在
                if (fi.Exists)
                {
                    //将文件保存到本机上
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                    Response.AddHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(fi.FullName);
                    Response.End();
                }
            }
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"alert", "<script>alert('请先选择需要下载的文件,谢谢!')</script>");
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        dFile();
    }
}