//前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
http://www.w3.org/1999/xhtml " >
//后台代码
using System;
Verwenden von System.Data;
Verwenden von System.Configuration;
mit System.Web;
mit System.Web.Security;
mit System.Web.UI;
mit System.Web.UI.WebControls;
mit System.Web.UI.WebControls.WebParts;
mit System.Web.UI.HtmlControls;
mit System.IO;
öffentlicher Teilklassentest: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string UploadFileName, UploadFileLastName;
UploadFileName = FileUpload1.PostedFile.FileName;//
UploadFileLastName = UploadFileName.Substring(UploadFileName.LastIndexOf(".") + 1);//得到文件的扩展名
Random rd = new Random();//产生随机数
int valationNo = 10 + rd.Next(99);//产生随机数
string suiji = valationNo.ToString();//产生随机数
string UpLoadFileTime = DateTime.Now.ToString("yyyyMMddHHmmss") + suiji;//
string NewUploadFileName = UpLoadFileTime + „.“ + UploadFileLastName;//产生上传图片的名称
string UserDirectory = "TestFile";//所要创建文件夹的名字,实际运用中可为用户注册ID
string UserPath = Server.MapPath("UploadFiles").ToString() + "\" + UserDirectory + "\" + "ProductsImages";
if (!Directory.Exists(UserPath)) //如果文件夹不存在则创建
{
Directory.CreateDirectory(UserPath);
}
FileUpload1.PostedFile.SaveAs(NewUploadPahtName);//用保存的方法将图片上载
string sUser_ProductsImageUrl = "UploadFiles/" + UserDirectory + "/" + "ProductsImages/" + NewUploadFileName;//
//Response.Write(sUser_ProductsImageUrl);
}
}
http://www.cnblogs.com/zm235/archive/2006/09/23/512673.html