fileupload应用 上传数据自己选择收件人选择上传的附件点发送后会创建一个文件夹存入你上传的附件

aspx中

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeFile="网易邮箱.aspx.cs" Inherits="网易邮箱" %>

<!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 runat="server">
        <tr><td></td><td><asp:Label ID="Label1" runat="server" Text="网易邮箱"></asp:Label></td></tr>
        <tr><td>发件人:</td><td>
            <asp:DropDownList ID="DropDownList2" runat="server" Height="20px" Width="135px">
                <asp:ListItem>李逸</asp:ListItem>
                <asp:ListItem>刘洪</asp:ListItem>
                <asp:ListItem>张展</asp:ListItem>
            </asp:DropDownList>
        </td></tr>
        <tr><td>收件人:</td><td>
            <asp:DropDownList ID="DropDownList1" runat="server" Height="44px" Width="135px">
                <asp:ListItem>王五</asp:ListItem>
                <asp:ListItem>张三</asp:ListItem>
                <asp:ListItem>河汉</asp:ListItem>
            </asp:DropDownList>
            </td></tr>
                <tr><td>
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td><td>
                        <asp:Label ID="Label2" runat="server"></asp:Label>
                    </td></tr>
                    <tr><td></td><td>
                        <asp:Button ID="Button2" runat="server" Text="发送" Xonclick="Button2_Click" />
                        <asp:Label ID="Label3" runat="server"></asp:Label>
                        </td></tr>
        </table>
        </div>
    </form>
</body>
</html>

cs中

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

public partial class 网易邮箱 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label2.Text += FileUpload1.PostedFile.FileName + " ";
        HttpFileCollection hf = Request.Files;
        for (int i = 0; i < hf.Count; i++)
        {
            HttpPostedFile hp = hf[i];
            if (hp.ContentLength > 0)
            {
                if (!Directory.Exists(@"E:\" + DropDownList2.Text)) //判断文件夹是否存在
                {
                    Directory.CreateDirectory(@"E:\" + DropDownList2.Text);//不存在则创建文件夹
                }
                    int a = hp.ContentLength;

                Label3.Text = a.ToString();
                //File.Create(@"E:\" + DropDownList2.Text, hp.ContentLength);
                string filename = hp.FileName;
                string path = @"E:\" + DropDownList2.Text;
                hp.SaveAs(path + @"\" +Path.GetFileName( filename));
               
            }
        }
    }
}

posted on 2012-10-02 22:31  gongth_12  阅读(487)  评论(0编辑  收藏  举报

导航