码迷,mamicode.com
首页 > Web开发 > 详细

html文件上传函数

时间:2020-12-02 12:30:08      阅读:12      评论:0      收藏:0      [点我收藏+]

标签:doc   server   mda   cti   xhr   serve   class   obj   als   

//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() + "文件上传了");
        }
    }
}

  

html文件上传函数

标签:doc   server   mda   cti   xhr   serve   class   obj   als   

原文地址:https://www.cnblogs.com/coolyylu/p/14050965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!