<td class="right">资料上传:</td>
<td style="height: 25px">
<asp:FileUpload ID="FileUpload1" runat="server" />(资料格式:rar,doc)
<asp:Button ID="BtnUploaf" runat="server" Enabled="false" Text="上传" οnclick="BtnUploaf_Click" />
<asp:Label ID="labFileName" runat="server" Visible="false" Text=""></asp:Label>
</td>
<system.web>
<!-- 配置上传压缩文件的大小(资料上传) -->
<httpRuntime maxRequestLength="51200" executionTimeout="600" />
//资料上传
protected void BtnUploaf_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
HttpPostedFile hpf = this.FileUpload1.PostedFile;
string FileSize = Convert.ToString(Convert.ToInt32(hpf.ContentLength.ToString()) / 1024);// +"KB";
//取得文件名(不含路径)
char[] de = { '//' };
string[] AFilename = hpf.FileName.Split(de);
// string strFilename = DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + AFilename[AFilename.Length - 1];
string strFilename = AFilename[AFilename.Length - 1];
string flag = strFilename.Substring(strFilename.LastIndexOf('.') + 1);
ViewState["FileFlag"] = flag;//文件的后缀名
ViewState["FileSize"] = FileSize; //文件的大小
if (strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "rar" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "doc"
|| strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "RAR" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "DOC")
{
hpf.SaveAs(Server.MapPath("/UploadBBSFile/" + strFilename));//上传至服务器
string dateStr = string.Format(DateTime.Now.ToString("yyyy") + @"/" + DateTime.Now.ToString("MM") + @"/" + DateTime.Now.ToString("dd") + @"/");
Session["FileName"] = strFilename; //资料名(原名)
this.labFileName.Text = strFilename;
ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(/"资料上传成功!/");</script>");
return;
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(/"资料格式不正确!/");</script>");
return;
}
}
}