前端代码:
使用visual studio开发实现文件上传
前端页面代码:
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"WebForm1.aspx.cs"
Inherits=
"scientist.WebForm1"
%>
<!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"
>
<script type=
"text/javascript"
>
var
baseText =
null
;
function upOpen() {
var
xzOpen = document.getElementById(
"xzOpen"
);
xzOpen.style.top =
"200px"
;
xzOpen.style.left =
"500px"
;
xzOpen.style.width =
"500px"
;
xzOpen.style.height =
"300px"
;
if
(baseText ==
null
) baseText = xzOpen.innerHTML;
xzOpen.innerHTML = baseText +
"<div id=\"statusbar\"><button onclick=\"hidePopup(); \">Close window<button></div>"
var
sbar = document.getElementById(
"statusbar"
);
sbar.style.marginTop = (parseInt(100)-20) +
"px"
;
xzOpen.style.visibility =
"visible"
;
document.getElementById(
"xzOpen"
).click();
}
</script>
<title>上传文档</title>
<style type=
"text/css"
>
*{ margin:0; padding:0;}
.exDiv{}
.boxmain{
float
:left;margin-right:0px;width:100%;}
.xzOpen{position: absolute; visibility: hidden; overflow: hidden; border:2px solid #CCC; background-color:
#FFCBB3; border:2px solid #333; padding:5px; }
.F1{
float
:left;margin-top:5px;}
.B1{
float
:right;margin-top:80px;}
.left{position:absolute;left:0; background:#BBFFBB;height:300px;width:20%}
.main{margin-right:200px;background:#79FF79; height:300px;;width:100%;margin-left:auto;}
.up{margin-right:1px;background:#984B4B; height:30px;width:64px;
margin-left:0px;
}
</style>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div
class
=
"exDiv"
style=
"width:100%; height:80px; margin:0 auto; border:solid 1px #999999;background-color:#95CACA"
>
<font >上传文件</font>
</div>
<div
class
=
"xzOpen"
id=
"xzOpen"
>
<div
class
=
"F1"
id=
"F1"
>
<asp:FileUpload ID=
"FileUpload1"
runat=
"server"
Width=
"224px"
/>
</div>
<div
class
=
"B1"
id=
"B1"
>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"提交"
onclick=
"Button1_Click"
style=
"margin:0 auto"
Width=
"107px"
/>
</div>
</div>
</form>
<div
class
=
"boxmain"
>
<div
class
=
"main"
>main</div>
</div>
<div
class
=
"left"
><input type=
"file"
id=
"xzFile"
style=
"display:none"
/>
<button type=
"button"
class
=
"up"
onclick=
"upOpen()"
>选择文件</button>
</div>
</body>
</html>
后台C#部分:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
scientist
{
public
partial
class
WebForm1 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
HttpPostedFile postedFile =
this
.FileUpload1.PostedFile;
String fileName =
""
;
String filePath =
"filesNameTest/"
;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if
(System.IO.Directory.Exists(Server.MapPath(filePath)) ==
false
)
{
System.IO.Directory.CreateDirectory(Server.MapPath(filePath));
}
if
(System.IO.File.Exists(Server.MapPath(filePath+fileName)) ==
true
)
{
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"message"
,
"alert('同名文件已存在')"
,
true
);
}
else
{
if
(fileName !=
""
)
{
String fileSuffix = System.IO.Path.GetExtension(fileName);
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(filePath) + fileName);
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"message"
,
"alert('已经保存成功')"
,
true
);
}
else
{
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"message"
,
"alert('请选择文件')"
,
true
);
}
}
}
}
}