zoukankan
html css js c++ java
个人学习代码保存:例5.利用标准FileUpload单文件上传
前台代码:Default.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<!
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
"
/>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Text
=
"
上传
"
OnClick
=
"
Button1_Click
"
/></
div
>
</
form
>
</
body
>
</
html
>
后台代码:Default.aspx.cs
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;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
if
(FileUpload1.HasFile)
{
string
FileName
=
FileUpload1.FileName;
//
取得文件类型
string
strFileExtend
=
FileName.Substring(FileName.LastIndexOf(
"
.
"
)
+
1
);
string
[] NoFileExtend
=
{
"
exe
"
,
"
php
"
,
"
asp
"
}
;
bool
Flag
=
true
;
for
(
int
i
=
0
; i
<
NoFileExtend.Length; i
++
)
{
if
(strFileExtend.Equals(NoFileExtend[i]))
{
Flag
=
false
;
}
}
if
(Flag)
{
string
strFilePath
=
string
.Format(
"
files/{0}.{1}
"
, DateTime.Now.ToString(
"
mmhhddss
"
), strFileExtend);
string
path
=
Server.MapPath(strFilePath);
FileUpload1.SaveAs(path);
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"
Startup
"
,
"
<script>alert('文件上传成功');</script>
"
);
Response.Write(
"
格式化后的地址为:
"
+
strFilePath
+
"
文件类型为:
"
+
strFileExtend);
}
else
{
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"
Startup
"
,
"
<script>alert('
"
+
string
.Format(
"
不能上传{0}格式文件
"
, strFileExtend)
+
"
');</script>
"
);
}
}
else
{
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"
Startup
"
,
"
<script>alert('文件不能为空');</script>
"
);
}
}
}
查看全文
相关阅读:
jslint报错太多的解决方式
gulp之文件合并以及整合html中的script和link
H5移动端下html上传图片被旋转问题
[转]如何处理iOS中照片的方向
[转]移动端上传图片翻转的解决方案
[转]【鹅厂网事】全局精确流量调度新思路-HttpDNS服务详解
phoneGap入门教程
[转]移动端HTML5<video>视频播放优化实践
[转]【技术心得】Last-Modified,Etag,Expire区别
[转]浏览器缓存机制
原文地址:https://www.cnblogs.com/wbcms/p/1036658.html
最新文章
常见面试题整理--Python概念篇
ubuntu安装jupyter 并设置远程访问
Ubuntu系统Python3相关环境或模块安装
MongoDB-6: MongoDB索引
MongoDB-5: 查询(游标操作、游标信息)
MongoDB-4: 查询(二-数组、内嵌文档)
MongoDB-3: 查询(一)
MongoDB-2:MongoDB添加、删除、修改
MongoDB-1:安装和配置
常用正则表达式
热门文章
C#
C#
[iOS] Baritem 添加一项
C#
Entify Framewrok
C#
WPF
NUnit
WPF
Window的匿名Closing 事件
Copyright © 2011-2022 走看看