//前台代码
<%@ Page 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="서버">
<title>无标题页</title>
</head>
<본체>
<form id="form1" runat="서버">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="input3" Width="290px"/>
<asp:Button ID="Button1" runat="server" Text="上传图文并创建文件夹" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
//后台代码
using System;
System.Data 사용;
System.Configuration 사용;
System.Web 사용;
System.Web.Security 사용;
System.Web.UI 사용;
System.Web.UI.WebControls 사용;
System.Web.UI.WebControls.WebParts 사용;
System.Web.UI.HtmlControls 사용;
System.IO 사용;
공개 부분 클래스 테스트 : System.Web.UI.Page
{
protected void Page_Load(개체 전송자, EventArgs e)
{
}
protected void Button1_Click(객체 전송자, EventArgs e)
{
문자열 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 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;//得到服务端图 Images 虚拟路径
//Response.Write(sUser_ProductsImageUrl);
}
}
http://www.cnblogs.com/zm235/archive/2006/09/23/512673.html