会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
Asp.Net技术前瞻--张明Blog
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
asp.net2.0 上传图片并为用户创建相应文件夹(原创)
Posted on
2006-09-23 15:13
张明
阅读(
2280
) 评论(
6
)
收藏
举报
(原创)上传图片并为用户创建相应文件夹
//
前台代码
<%
@ 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
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:FileUpload ID
=
"
FileUpload1
"
runat
=
"
server
"
CssClass
=
"
input3
"
Width
=
"
290px
"
/>&
nbsp;
&
nbsp;
&
nbsp;
&
nbsp;
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Text
=
"
上传图片并创建文件夹
"
OnClick
=
"
Button1_Click
"
/>
</
div
>
</
form
>
</
body
>
</
html
>
//
后台代码
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.IO;
public
partial
class
test : 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);
}
}
刷新页面
返回顶部
博客园
© 2004-2025
浙公网安备 33010602011771号
浙ICP备2021040463号-3