客户端:
页面:加入一个FileUpload 控件,一个Button(btnUpload)
下面是btnUpload的事件:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<script runat="server">
</script>
<body>
<form id="form1" runat="server">
<div>
<ASP:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" /> <br />
<br />
<ASP:RegularExpressionValidator
id="RegularExpressionValidator1" runat="server"
ErrorMessage="Only mp3,m3u or mpeg files are allowed!"
//下面的文件类型帅选代码,可以在aspx.cs页面中写更为妥当
ValidationExpression="^(([a-zA-Z]:)|(//{2}/w+)/$?)(//(/w[/w].*))+(.jpg|.JPG|.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$"
ControlTovalidate="FileUpload1"></ASP:RegularExpressionValidator>
<br />
<ASP:requiredFieldValidator
id="requiredFieldValidator1" runat="server"
ErrorMessage="This is a required field!"
ControlTovalidate="FileUpload1"></ASP:requiredFieldValidator>
<br />
<ASP:Label ID="Label1" runat="server"></ASP:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
protected void Button1_Click(object sender,EventArgs e)
{
if (FileUpload1.HasFile)
{
//string clientpath = FileUpload1.PostedFile.FileName.ToString();
if(FileUpload1.PostedFile.ContentLength!=0)
{
try
{
string pathfirst = "C://Uploads//" + FileUpload1.FileName;
FileUpload1.SaveAs("C://Uploads//" +
FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
string sql = "insert into test(path) values('" + pathfirst + "')";
sqlConnection con = new sqlConnection("Data Source=192.168.1.69;Initial Catalog=calendar;Integrated Security=False;user ID=sa;Password=huison");
sqlCommand com = new sqlCommand(sql,con);
con.open();
com.ExecuteNonQuery();
con.Close();
Label2.Text = FileUpload1.FileName;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "This file does not exist.";
}
}
else
{
Response.Write("You have not specified a file.");
} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。