zoukankan
html css js c++ java
asp.net文件上传
1.html
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
单文件上传
<
br
/>
<
br
/>
<
input
id
="File1"
runat
="server"
type
="file"
/>
<
br
/>
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="Button"
/>
<
br
/>
<
asp:Label
ID
="LblMsg"
runat
="server"
Height
="256px"
Width
="100%"
></
asp:Label
>
</
div
>
</
form
>
</
body
>
</
html
>
2.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
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
other_ShangChuan1 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{ }
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
if
(File1.PostedFile.FileName
!=
""
)
{
//
上传文件的绝对路径 如:C:\12.jpg
string
sFile
=
File1.PostedFile.FileName;
//
获取文件全名 例:12.jpg
sFile
=
sFile.Substring(sFile.LastIndexOf(
"
\\
"
)
+
1
);
//
获取后缀名 例 .jpg
sFile
=
sFile.Substring(sFile.LastIndexOf(
"
.
"
));
//
为了防止重名,获得日期为文件名 年月日时分秒毫秒
string
datatime
=
System.DateTime.Now.ToString(
"
yyyMMddHHmmssffff
"
);
//
上传后文件的新名
sFile
=
datatime
+
sFile;
//
AppDomain.CurrentDomain.BaseDirectory.ToString() 获取此项目的根目录
//
sPath 获取上传后的路径
string
sPath
=
AppDomain.CurrentDomain.BaseDirectory.ToString()
+
"
uploads\\
"
+
sFile;
//
上传文件
File1.PostedFile.SaveAs(sPath);
this
.LblMsg.Text
=
"
文件已经上传到:
"
+
sPath;
this
.LblMsg.Text
+=
"
<br/>上传文件名称:
"
+
this
.File1.PostedFile.FileName;
this
.LblMsg.Text
+=
"
<br/>上传文件类型:
"
+
this
.File1.PostedFile.ContentType;
this
.LblMsg.Text
+=
"
<br/>上传文件大小:
"
+
this
.File1.PostedFile.ContentLength
+
"
Byte
"
;
}
else
{
this
.LblMsg.Text
=
"
请选择需要上传的文件?
"
;
}
}
}
查看全文
相关阅读:
selenium的持续问题解决
为什么使用Nginx
[转]性能测试场景设计深度解析
[转]CentOS7修改时区的正确姿势
[转]利用Fiddler模拟恶劣网络环境
[转]什么是微服务
[转] WebSocket 教程
[转] Python实现简单的Web服务器
shell修改配置文件参数
[转] linux shell 字符串操作(长度,查找,替换)详解
原文地址:https://www.cnblogs.com/puke/p/782513.html
最新文章
sqlserver 修改表结构提示阻止
Invoke
mvc dropdown
Net User 命令
System.Xml.XPath.XPathException
TcpIp
get--ip
跨域获取json电商数据
SQL語句性能比較
转(Check Null Before Adding into list)
热门文章
文件上传怎么测试
题记
白盒测试day03
白盒测试day02
白盒测试day01
软件测试的数据库技术02
软件测试的数据库技术01
git安装与使用
shell语法
Linux常用软件安装
Copyright © 2011-2022 走看看