• 多文件上传方法


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

                }

                        

  • 相关阅读:
    nodejs pm2使用
    react生命周期
    It's a start!
    关于react-native报错: Invariant Violation: WebView has been removed from React Native. It can ....
    react-native 页面使用WebView布满整个页面,导航栏不显示问题
    两个函数执行顺序,异步问题处理(已解决)
    react-native webView乱码问题
    react-native 多页面之间传值
    时间戳转换为时间日期格式
    React Native返回刷新页面(this.props.navigation.goBack())
  • 原文地址:https://www.cnblogs.com/baozi/p/3222626.html
Copyright © 2020-2023  润新知