zoukankan      html  css  js  c++  java
  • html文件上传函数

    //fileupload.js
    function select_upload(zurl, zmulti, callback) {
        var zinput_file = document.createElement("input");
        zinput_file.type = "file";
        zinput_file.multiple = zmulti;
        zinput_file.onchange = function (e) {
    
            var form = new FormData();
            for (var zi = 0; zi < zinput_file.files.length; zi++) {
                form.append("file[" + zi.toString() + "]", zinput_file.files[zi]);
            }
    
            
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    callback(xhr.responseText);
                }
            };
    
            xhr.open("post", zurl);
            xhr.send(form);
            
        }
        zinput_file.click();
    }
    

      

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="fileupload.js"></script>
    </head>
    <body>
    <input type="button" value="上传文件" id="btnupload" onclick="upload_file();" />
    <script type="text/javascript">
        function upload_file() {
            
            select_upload("upload.aspx", true, function (text) {
                alert(text);
            });
        }
    </script>
    </body>
    </html>
    
    //upload.aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace testfileupload
    {
        public partial class upload : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Request.Files.Count > 0) {
                    string zpath = Server.MapPath("upload");
    
                    for (int i = 0; i < Request.Files.Count; i++) {
                        Request.Files[i].SaveAs(System.IO.Path.Combine(zpath, Request.Files[i].FileName));
                    }
                }
                Response.Write(Request.Files.Count.ToString() + "文件上传了");
            }
        }
    }
    

      

    复杂版本的上传js函数

    function select_upload(zurl, zmulti, sucess_callback ,ztag_name,open_callback,error_callback) {
        var zinput_file = document.createElement("input");
        zinput_file.type = "file";
        zinput_file.multiple = zmulti;
        if (!ztag_name) {
            ztag_name = "file";
        }
        zinput_file.onchange = function (e) {
    
            var form = new FormData();
            if (zmulti) {
                for (var zi = 0; zi < zinput_file.files.length; zi++) {
                    form.append(ztag_name+"[" + zi.toString() + "]", zinput_file.files[zi]);
                }
            } else {
                form.append(ztag_name, zinput_file.files[0]);
            }
    
            if (open_callback) {
                open_callback("");
            }
    
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
                        if (xhr.status == 200) {
                            sucess_callback(xhr.responseText);
                        }
                        
                    } else {
                        if (error_callback) {
                            error_callback(xhr.status);
                        }
                    }
                }       
            };
    
            xhr.open("post", zurl);
            xhr.send(form);
    
        }
        zinput_file.click();
    }
    

      

  • 相关阅读:
    价值理论的出发点和落脚点都是人--以人为本
    价值理论是人类决策和行为的标尺
    事实判断和价值判断
    什么是价值理论?---人们认识世界和改造世界的过程可分解为四个基本阶段
    大人只看利弊 小孩才分对错
    为人处世、事实判断和价值判断皆不可少--人类认识客观事物的标尺:对错与利弊
    知行之间--价值与真理--行动的标尺
    事实判断与价值判断之间的桥梁就是人的需要
    10分钟梳理MySQL核心知识点
    postman设置环境变量,实现一套接口根据选择的环境去请求不同的url
  • 原文地址:https://www.cnblogs.com/coolyylu/p/14050965.html
Copyright © 2011-2022 走看看