<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
// var str = "<input type='file' name='files'/><br/>";
var mydiv = document.getElementById("divFiles");
// mydiv.insertAdjacentHTML("beforeEnd", str);
var inp = document.createElement("input");
inp.type = "file";
inp.name = "files";
mydiv.appendChild(inp);
}
// ]]>
</script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
text-align: right;
width: 62px;
}
.style3
{
width: 62px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
发信人:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="fajianren"
DataValueField="fajianren">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:邮件ConnectionString %>"
SelectCommand="SELECT [fajianren] FROM [Table_1]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="style2">
收信人:</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource2" DataTextField="shoujianren"
DataValueField="shoujianren">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:邮件ConnectionString2 %>"
SelectCommand="SELECT [shoujianren] FROM [Table_2]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="style2">
主题:</td>
<td>
<asp:TextBox ID="txtzhuti" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<input id="Button1" type="button" value="添加附件" onclick="return Button1_onclick()" />
<asp:Button ID="Button4" runat="server" Text="上传" onclick="Button4_Click" />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<div id="divFiles">
</div>
</td>
</tr>
<tr>
<td class="style2">
正文:</td>
<td>
<asp:TextBox ID="txtneirong" runat="server" Height="197px" Width="441px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Button ID="Button3" runat="server" Text="发送" onclick="Button3_Click" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Request.Files.Count; i++)
{
HttpPostedFile file = this.Request.Files[i];
string filename = System.IO.Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/") + filename);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("userimage") + "/" + DropDownList1.Text + "-to-" + DropDownList2.Text)) //判断是否文件存在,存在就不执行创建命令了
{
Directory.CreateDirectory(Server.MapPath("userimage") + "/" + DropDownList1.Text + "-to-" + DropDownList2.Text);
}
for (int i = 0; i < Request.Files.Count; i++) //进行所有用用动态添加的fileupload控件集合的遍历,分别进行存储
{
HttpPostedFile file = Request.Files[i];
string filename = System.DateTime.Now.Ticks.ToString() + System.IO.Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("userimage") + "/" + DropDownList1.Text + "-to-" + DropDownList2.Text + "/" + filename);
}
}
}