zoukankan      html  css  js  c++  java
  • 上传文档 FileUpload

    前台页面:

     <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>

    后台代码:

    protected void Button1_Click(object sender, EventArgs e)
            {
                NewMethod();
            }
    
            private void NewMethod()
            {
                //上传文件到指定文件夹
                string fileName = "";
                string fileExt = "";
                string strName = "";
    
                //从上传的文件流中提取所有文件并保存到数据库
                for (int i = 0; i < this.Request.Files.Count; i++)
                {
                    fileName = this.Request.Files[i].FileName.Substring(this.Request.Files[i].FileName.LastIndexOf("\\") + 1);
                    fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
    
                    if (IsUploadFile(fileName.Substring(fileName.LastIndexOf(".") + 1)))
                    {
                        byte[] fileData = new byte[this.Request.Files[i].ContentLength];
                        this.Request.Files[i].InputStream.Read(fileData, 0, this.Request.Files[i].ContentLength);
    
                        var Files = Request.Files[i];
                        try
                        {
                            strName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + fileExt;
                            Files.SaveAs(Server.MapPath("../Upload/file/") + strName);
    
                        }
                        catch (Exception ex)
                        {
                            throw (ex);
                        }
    
                    }
    
                    else
                    {
                        string msg = "只能上传office、pdf及压缩文件!";
                        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(),"alert('" + msg + "');", true);
                        return;
    
                    }
    
                }
    
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(),"alert('文件上传成功');", true);
            }
    
    public bool IsUploadFile(string fileExt) { if (fileExt != "") { if (fileExt != "doc" && fileExt != "docx" && fileExt != "xls" && fileExt != "xlsx" && fileExt != "ppt" && fileExt != "pptx" && fileExt != "pdf" && fileExt != "rar" && fileExt != "zip") { return false; } else { return true; } } else { return false; } }
  • 相关阅读:
    bootstrap之Click大事
    BZOJ 2878([Noi2012]-失落的游乐园树DP+出站年轮加+后市展望DP+vector的erase)
    cocos2d 消除类游戏简单的算法 (一)
    【BZOJ3627】【JLOI2014】路径规划 分层图
    Windows台cocos2d-x 3.2下载一个新的项目,创造的过程
    无插件,直接加参数,chrome它可以模拟手机浏览器
    unix您不能使用crontab设置运营计划
    LeetCode36:Valid Sudoku
    HDInsight HBase概观
    最受欢迎telnet
  • 原文地址:https://www.cnblogs.com/JiangXiaoTian/p/2699801.html
Copyright © 2011-2022 走看看