C#文件上传功能

<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;
                }
            }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值