上传文件
我要问的问题是:当我上传文件时,当然文件可以为空,但是如果选择了文件,那么文件的格式就要正确,如果不正确时,就弹出一个框说选择的文件格式不正确,并且此时不进行数据存储,也就是不让上传按钮触发事件,这怎么实现呢?请各位给与指点,谢谢了。我的源代码如下:
private void btnUpload_Click(object sender, System.EventArgs e)
{
//上传音乐
string fullFileName=this.uploadMusicname.PostedFile.FileName;
string fileName=fullFileName.Substring(fullFileName.LastIndexOf("\\")+1);
string type=fullFileName.Substring(fullFileName.LastIndexOf(".")+1);
if(type=="mp3"||type=="wav"||type=="mp4"||type=="avi"||type=="wma"||type=="rm"||type=="rmvb"||type=="dat")
{
this.uploadMusicname.PostedFile.SaveAs(Server.MapPath("~/uploadMusic")+"\\"+fileName);
}
else
{
Response.Write("<script>alert('你选择的歌曲的格式不正确!');</script>");
}
string filePath="uploadMusic/"+fileName;
//上传歌词
string fullFileName1=this.uploadMusiclyric.PostedFile.FileName;
string fileName1=fullFileName1.Substring(fullFileName1.LastIndexOf("\\")+1);
string type1=fullFileName1.Substring(fullFileName1.LastIndexOf(".")+1);
if(type1=="lrc"||type1=="txt"||type1=="doc")
{
this.uploadMusiclyric.PostedFile.SaveAs(Server.MapPath("~/uploadMusic")+"\\"+fileName1);
}
else
{
Response.Write("<script>alert('你选择的歌词的格式不正确!');</script>");
}
string filePath1="uploadMusic/"+fileName1;
//获取文本框中的值
string gqmz=this.txtMusicname.Text.Trim();
string gqycz=this.txtMusicsinger.Text.Trim();
string gqzj=this.txtMusicspecial.Text.Trim();
string gqfbsj=this.txtMusicreleasetime.Text.Trim();
//进行插入操作
SqlConnection con=new SqlConnection(Application["conStr"].ToString());
con.Open();
string insertstr="insert into music(musicName,musicLyricpath,musicSinger,musicPath,musicSpecial,musicReleasetime,musicHits) values('"+gqmz+"','"+filePath1+"','"+gqycz+"','"+filePath+"','"+gqzj+"','"+gqfbsj+"',0)";
SqlCommand cmd=new SqlCommand(insertstr,con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
Response.Write("有错误!");
}
cmd.Dispose();
con.Close();
con.Dispose();
this.lblState.Text="上传成功!";
this.bindTodgMusic();
this.txtMusicname.Text="";
this.txtMusicreleasetime.Text="";
this.txtMusicsinger.Text="";
this.txtMusicspecial.Text="";
}
[此贴子已经被作者于2007-5-18 13:20:33编辑过]