当前位置:编程学习 > C#/ASP.NET >>

ASP.NET(C#)

答案:
一个生成不重复随机数的方法
//生成不重复随机数算法
private int GetRandomNum(int i,int length,int up,int down)
{
int iFirst=0;
Random ro=new Random(i*length*unchecked((int)DateTime.Now.Ticks));
iFirst=ro.Next(up,down);
return iFirst;
}
发表于 @ 2005年12月30日 3:44 PM | 评论 (0)

ASP.NET多文件上传方法
前台代码

<script language="Javascript">
function addFile()
{
var str = '<INPUT type="file" size="30" NAME="File"><br>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)

}
</script>

<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<P id="MyFile"><INPUT type="file" size="30" NAME="File"><br></P>
<P>
<input type="button" value="增加(Add)" onclick="addFile()"> <input onclick="this.form.reset()" type="button" value="重置(ReSet)">
<asp:Button Runat="server" Text="开始上传" ID="UploadButton">
</asp:Button>
</P>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" Width="500px"
BorderStyle="None" BorderColor="White"></asp:Label>
</P>
</div>
</form>


后台代码


protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;

private void Page_Load(object sender, System.EventArgs e)
{
/// 在此处放置用户代码以初始化页面
if (this.IsPostBack) this.SaveImages();
}

private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;

/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName,fileExtension,file_id;

//取出精确到毫秒的时间做文件的名称
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString()+iFile.ToString();

fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = my_file_id+fileExtension;
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + file_id + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + file_id);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}


发表于 @ 2005年12月30日 3:37 PM | 评论 (0)

邮件系统使用的上传附件方法
前台HTML代码


<form id="mail" method="post" runat="server">
<table border="0" cellpadding="0" cellspacing="0" style="BORDER-COLLAPSE: collapse" bordercolor="#111111"
width="39%" id="AutoNumber1" height="75">
<tr>
<td width="100%" height="37"><INPUT id="myFile" style="WIDTH: 297px; HEIGHT: 22px" type="file" size="30" name="myFile"
runat="server">
<asp:button id="Upload" runat="server" Text="上传附件"></asp:button></td>
</tr>
<tr>
<td width="100%" height="38">
共计
<asp:textbox id="P_size" runat="server" Width="65px"></asp:textbox>KB

<asp:dropdownlist id="dlistBound" runat="server"></asp:dropdownlist>
<asp:button id="btnDel" runat="server" Text="删除附件"></asp:button></td>
</tr>
</table>
</form>


后台CS代码


public class Upload_Mail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.WebControls.DropDownList dlistBound;
protected System.Web.UI.WebControls.TextBox P_size;
protected System.Web.UI.WebControls.Button btnDel;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;

private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{
//没有附件的状态
dlistBound.Items.Clear();
ArrayList arr = new ArrayList();
arr.Add("--没有附件--");
dlistBound.DataSource = arr ;
dlistBound.DataBind();
P_size.Text = "0";
}
}

private void Upload_Click(object sender, System.EventArgs e)
{
if(myFile.PostedFile !=null)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
string fileName = System.IO.Path.GetFileName(postedFile.FileName);
string path = Request.PhysicalApplicationPath+@"UploadMail\"+ fileName;
postedFile.SaveAs(path);

//数组对上存附件进行实时绑定
if((string)Session["udMail"]==null)
{
Session["udMail"] = fileName;
}
else
{
Session["udMail"] = (string)Session["udMail"]+"|"+fileName;
}

string[] udMail = Session["udMail"].ToString().Split('|');
ArrayList list = new ArrayList(udMail);
list.Reverse();
udMail=(string[])list.ToArray(typeof(string));
dlistBound.Items.Clear();
long dirsize=0;
for(int i = 0;i<udMail.Length;i++)
{
string IndexItem = udMail[i];
string VauleItem = Request.PhysicalApplicationPath+@"UploadMail\"+udMail[i];
dlistBound.Items.Add(new ListItem(IndexItem,VauleItem));
System.IO.FileInfo mysize = new System.IO.FileInfo(@VauleItem);
dirsize += System.Convert.ToInt32(mysize.Length/1024)+1;
}
P_size.Text = dirsize.ToString();

}
}

private void btnDel_Click(object sender, System.EventArgs e)
{
string trueDelfile = dlistBound.SelectedValue.ToString();
string Delfile = dlistBound.SelectedItem.ToString();
usageIO.DeletePath(trueDelfile);

if(Session["udMail"] != null)
{
int index = Session["udMail"].ToString().IndexOf("|");
if(index == -1)
{
Session["udMail"] = null;
dlistBound.Items.Clear();
dlistBound.Items.Add("--没有附件--");

上一个:几个C#常用正则表达式的总结
下一个:页面包含的处理

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,