zoukankan
html css js c++ java
关于文件的上传。
asp.net里的代码:
<
form
id
="Form1"
method
="post"
runat
="server"
enctype
="multipart/form-data"
>
<
input
type
="file"
runat
="server"
id
="UploadImage"
name
="UploadImage"
>
<
asp:Button
id
="Button1"
runat
="server"
Text
="Button"
></
asp:Button
>
<
asp:Label
id
="lblMsg"
runat
="server"
ForeColor
="Red"
></
asp:Label
>
</
form
>
button的事件如下:
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
string
all_filename,this_filename,ext_filename,new_filename;
if
(UploadImage.PostedFile.ContentLength
==
0
)
{
this
.lblMsg.Text
=
"
该文件不存在。
"
;
}
else
{
all_filename
=
UploadImage.PostedFile.FileName;
string
[] a
=
all_filename.Split(
"
\\
"
.ToCharArray());
this_filename
=
a[a.Length
-
1
].ToString();
ext_filename
=
this_filename.Remove(
0
,this_filename.LastIndexOf(
"
.
"
));
new_filename
=
System.Guid.NewGuid()
+
ext_filename;
UploadImage.PostedFile.SaveAs(Server.MapPath(
"
/sourcefile
"
)
+
@"
\c1\
"
+
new_filename);
}
}
以上是对单个文件的上传的方法,如果要同时实现多文件的上传。代码如下:
这是asp.net页面里的代码,注意新添加的file可以不需要id和name的
<
form
id
="Form1"
method
="post"
encType
="multipart/form-data"
runat
="server"
>
<
P
><
input
id
="UploadImage"
type
="file"
name
="UploadImage"
runat
="server"
><
br
>
<
input
type
="file"
runat
="server"
><
br
>
<
input
type
="file"
runat
="server"
></
P
>
<
P
>
<
asp:Button
id
="Button1"
runat
="server"
Text
="Button"
></
asp:Button
>
<
asp:Label
id
="lblMsg"
runat
="server"
ForeColor
="Red"
></
asp:Label
></
P
>
</
form
>
c#代码部分
{
for
(
int
i
=
0
;i
<
Request.Files.Count;i
++
)
{
System.Web.HttpPostedFile myfile
=
Request.Files[i];
string
all_filename,this_filename,ext_filename,new_filename;
if
(myfile.ContentLength
!=
0
)
{
all_filename
=
myfile.FileName;
string
[] a
=
all_filename.Split(
"
\\
"
.ToCharArray());
this_filename
=
a[a.Length
-
1
].ToString();
ext_filename
=
this_filename.Remove(
0
,this_filename.LastIndexOf(
"
.
"
));
new_filename
=
System.Guid.NewGuid()
+
ext_filename;
myfile.SaveAs(Server.MapPath(
"
/sourcefile
"
)
+
@"
\c1\
"
+
new_filename);
}
}
}
查看全文
相关阅读:
[BZOJ1625][Usaco2007 Dec]宝石手镯
[BZOJ1699][Usaco2007 Jan]Balanced Lineup排队
[BZOJ1606][Usaco2008 Dec]Hay For Sale 购买干草
[BZOJ1610][Usaco2008 Feb]Line连线游戏
[BZOJ1609][Usaco2008 Feb]Eating Together麻烦的聚餐
[BZOJ1602][Usaco2008 Oct]牧场行走
[BZOJ1601][Usaco2008 Oct]灌水
[BZOJ1607][Usaco2008 Dec]Patting Heads 轻拍牛头
[BZOJ1579][Usaco2008 Mar]土地购买
HDU 4248 A Famous Stone Collector 组合数学dp ****
原文地址:https://www.cnblogs.com/songafeng/p/168801.html
最新文章
设置TabBar图片
淘宝bug bug bug
AppleScript脚本学习记录《二》
AppleScript脚本学习记录《一》
Linux下安装Tomcat
http8种请求方式
Ubuntu14.4配置vnc
Java环境配置
Cobbler批量安装操作系统
如何通俗地理解docker
热门文章
虚拟化技术kvm,xen,vmware比较
centos vnc配置总结
持续集成 部署 交付
Ubuntu SVN 搭建
Vi方向键变乱码 退格键不能使用
svn搭建
mysql主从复制
shell printf命令:格式化输出语句
VIM使用技巧
[Usaco2007 Jan]Running贝茜的晨练计划
Copyright © 2011-2022 走看看