zoukankan      html  css  js  c++  java
  • 多文件上传方法

    1.js代码 

    function uploadFile() {
                var str = '<br/><INPUT type="file" size="30" NAME="File">'
                document.getElementById('MyFile').insertAdjacentHTML("beforeEnd", str)
            }

    2.页面含义html代码(页面必须含有一个runat=“server”的file对象)

       <p id="MyFile"> <input type="file" size="30" name="File" runat="server"></p> <input type="button" value="增加(Add)" onclick="uploadFile()">

    3.后台页面代码

       public bool attachfileupload()
            {
                Hashtable ht = new Hashtable();
                HttpFileCollection files = HttpContext.Current.Request.Files;
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    HttpPostedFile postedFile = files[iFile];
                    string filePathName = postedFile.FileName;
                    string fileName = Path.GetFileName(filePathName);
                    string fileExtension = Path.GetExtension(filePathName);
                    if (fileExtension == ".jpg" || fileExtension == ".bmp" || fileExtension == ".gif" || fileExtension == ".png" || fileExtension == ".txt" || fileExtension == ".docx" || fileExtension == ".doc" || fileExtension == ".xls" || fileExtension == ".rar")
                    {
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(Page.GetType(), "filetype", "alert('"+filePathName+"类型错误,请选择正确的文件类型')", true);
                        return false;
                    }
                }
                //文件类型正确
                //文件大小
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    HttpPostedFile postedFile = files[iFile];
                    string filePathName = postedFile.FileName;
                    string fileName = Path.GetFileName(filePathName);
                    string fileExtension = Path.GetExtension(filePathName);
                    if (postedFile.ContentLength < 5120000)
                    {
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(typeof(string), "filetye", "alert('"+filePathName+"文件大小超出范围')", true);
                        return false;
                    }
                }
                //上传文件
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    HttpPostedFile postedFile = files[iFile];
                    string filePathName = postedFile.FileName;
                    string fileName = Path.GetFileName(filePathName);
                    string fileExtension = Path.GetExtension(filePathName);
                    string fileid = System.Guid.NewGuid().ToString()  + fileExtension;

                    postedFile.SaveAs(Server.MapPath(@"~/upload/" + fileid));

                }

                        

  • 相关阅读:
    VSS部署手册
    正则表达式学习(二)
    完全卸载oracle11g步骤
    c#中 命令copy 已退出,返回值为1
    Windows 64位下装Oracle 11g,PLSQL Developer的配置问题,数据库处显示为空白的解决方案
    ora01033和ora12560错误的解决方案
    C#DLL加密保护
    ocslive.conf
    ASP.NET中文乱码问题的解决
    Creating a very simple autorestore USB stick clonezilla
  • 原文地址:https://www.cnblogs.com/baozi/p/3222626.html
Copyright © 2011-2022 走看看