//前台代码
<%@ Halaman Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!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>
<tubuh>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="input3" width="290px"/>
<asp:Button ID="Button1" runat="server" Text="上传图片并创建文件夹" OnClick="Button1_Click" />
</div>
</bentuk>
</tubuh>
</html>
//后台代码
menggunakan System;
menggunakan Sistem.Data;
menggunakan Sistem.Konfigurasi;
menggunakan Sistem.Web;
menggunakan Sistem.Web.Keamanan;
menggunakan Sistem.Web.UI;
menggunakan Sistem.Web.UI.WebControls;
menggunakan System.Web.UI.WebControls.WebParts;
menggunakan Sistem.Web.UI.HtmlControls;
menggunakan Sistem.IO;
tes kelas parsial publik : System.Web.UI.Page
{
dilindungi kekosongan Page_Load (pengirim objek, EventArgs e)
{
}
void yang dilindungi Button1_Click(pengirim objek, 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 NamaUploadFile Baru = WaktuUploadFile+"." + UploadFileLastName;//产生上传图片的名称
string UserDirectory = "TestFile";//所要创建文件夹的名字,实际运用中可为用户注册ID
string UserPath = Server.MapPath("UploadFiles").ToString() + "\" + UserDirectory + "\" + "ProductsImages";
if (!Directory.Exists(UserPath)) //如果文件夹不存在则创建
{
Direktori.CreateDirectory(UserPath);
}
FileUpload1.PostedFile.SaveAs(NewUploadPahtName);//用保存的方法将图片上载
string sUser_ProductsImageUrl = "UploadFiles/" + UserDirectory + "/" + "ProductsImages/" + NewUploadFileName;//得到服务端图片的虚拟路径
//Respon.Tulis(sUser_ProductsImageUrl);
}
}
http://www.cnblogs.com/zm235/archive/2006/09/23/512673.html