会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
用AspJpeg组件,按宽高比例,真正生成缩略图
laifangsong's .NET blog
取长补短,精益求精。 (打个广告,想做手机网站和asp/asp.net网站的可以跟我联系.QQ:25313644)
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
单/多文件上传。
Posted on
2006-04-17 14:51
laifangsong
阅读(
1115
) 评论(
3
)
编辑
收藏
举报
using
System;
using
System.IO;
using
System.Configuration;
using
System.Web;
namespace
AspNetTest.Common
{
/**/
///
<summary>
///
单/多文件上传,,写的不好,见笑。.
///
</summary>
public
class
UploadFile
{
public
UploadFile()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
//
上传文件默认目录,例如: UploadFiles,该值在web.config中配置
private
string
initDirectory
=
ConfigurationSettings.AppSettings[
"
Upload_InitDir
"
];
//
是否启用决定路径,该路径一般会存在数据库中。若设为绝对路径,调用不用考虑相对路径问题
private
bool
useAbsolutePath
=
true
;
//
上传路径,例如:上传路径是 Pic,那么所有的图片都会存在 UploadFiles/Pic目录下,为空时全部存在 UploadFiles目录下
private
string
uploadPath
=
""
;
//
是否启用随机文件名,随机文件名的格式是:yyyyMMddhhmmss + 3位数的随机数
private
bool
useRandFileName;
//
是否自动创建日期目录,若启用,会在上传文件目录下生成 2006/4/17 这3个目录
private
bool
createDateDir;
public
bool
UseAbsolutePath
{
get
{
return
useAbsolutePath;
}
set
{
useAbsolutePath
=
value;
}
}
public
string
UploadPath
{
get
{
return
uploadPath;
}
set
{
if
(UseAbsolutePath)
{
uploadPath
=
GetApplicationPath();
}
uploadPath
+=
this
.initDirectory
+
"
/
"
;
string
valueExtended
=
value.ToString().Trim();
if
(valueExtended
!=
""
)
{
uploadPath
+=
value;
if
(
!
valueExtended.EndsWith(
"
/
"
))
{
uploadPath
+=
"
/
"
;
}
}
}
}
public
bool
UseRandFileName
{
get
{
return
useRandFileName;
}
set
{
useRandFileName
=
value;
}
}
public
bool
CreateDateDir
{
get
{
return
createDateDir;
}
set
{
createDateDir
=
value;
}
}
public
void
Upload()
{
HttpFileCollection hpc
=
HttpContext.Current.Request.Files;
if
(hpc
!=
null
)
{
for
(
int
i
=
0
; i
<
hpc.Count; i
++
)
{
HttpPostedFile file
=
hpc[i];
//
判断大小
int
fileContentLength
=
file.ContentLength;
if
(fileContentLength
<
1
)
{
continue
;;
}
int
allowMaxSize
=
Convert.ToInt32(ConfigurationSettings.AppSettings[
"
Upload_AllowMaxSize
"
]);
if
(fileContentLength
>
allowMaxSize
*
1024
*
1024
)
{
HttpContext.Current.Response.Write(
"
最大上传的文件只允许<b>
"
+
Convert.ToString(allowMaxSize
/
1000
)
+
"
M</b>
"
);
return
;
}
string
fileName
=
Path.GetFileName(file.FileName);
string
fileContentType
=
Path.GetExtension(fileName).Remove(
0
,
1
);
string
allowTypes
=
ConfigurationSettings.AppSettings[
"
Upload_AllowTypes
"
];
//
判断类型
if
((
"
|
"
+
allowTypes.ToLower()
+
"
|
"
).IndexOf(
"
|
"
+
fileContentType
+
"
|
"
)
==
-
1
)
{
HttpContext.Current.Response.Write(
"
只允许上传的文件格式有:<b>
"
+
allowTypes
+
"
</b>
"
);
return
;
}
//
随机文件名
if
(UseRandFileName)
{
fileName
=
DateTime.Now.ToString(
"
yyyyMMddhhmmss
"
)
+
(
new
Random()).Next(
100
,
1000
).ToString()
+
"
.
"
+
fileContentType;
}
string
addPath
=
HttpContext.Current.Server.MapPath(UploadPath);
if
(
!
Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
//
创建日期目录
if
(CreateDateDir)
{
addPath
+=
DateTime.Today.Year.ToString()
+
@"
\
"
;
if
(
!
Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
addPath
+=
DateTime.Today.Month.ToString().PadLeft(
2
,
'
0
'
)
+
@"
\
"
;
if
(
!
Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
addPath
+=
DateTime.Today.Day.ToString().PadLeft(
2
,
'
0
'
)
+
@"
\
"
;
if
(
!
Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
}
string
filePath
=
addPath
+
fileName;
file.SaveAs((filePath));
HttpContext.Current.Response.Write(
"
<br>文件(<b>
"
+
fileName
+
"
</b>)上传成功!<br>地址:
"
);
HttpContext.Current.Response.Write(filePath
+
"
<hr>
"
);
WriteFilePathToDB(filePath);
}
}
}
public
void
WriteFilePathToDB(
string
path)
{
//
将上传文件路径写入数据库
}
private
string
GetApplicationPath()
{
string
appPath
=
HttpContext.Current.Request.ApplicationPath;
if
(
!
appPath.EndsWith(
"
/
"
))
{
appPath
+=
"
/
"
;
}
return
appPath;
}
}
}
调用:
UploadFile oUploadFile
=
new
UploadFile();
oUploadFile.UseAbsolutePath
=
true
;
oUploadFile.UploadPath
=
"
Pic
"
;
oUploadFile.CreateDateDir
=
true
;
oUploadFile.UseRandFileName
=
true
;
oUploadFile.Upload();
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 laifangsong
Powered by .NET 8.0 on Kubernetes