zoukankan      html  css  js  c++  java
  • h5的formData 上传文件及.net后台

    先来前端的代码:

    html 代码:

    <input type="file" id="files" value="" multiple/>

    js代码:

    function init() {
        var ele_files = document.querySelector("#files");
        ele_files.addEventListener("change", function () {var files = ele_files.files;
            var form = new FormData();
            for (var i = 0; i < files.length; i++) {
                form.append("files"+i, files[i]);
            }
            $ajax("/ashx/handle.ashx", "post", form, function (data) { console.log(data); });
        }, false);
    }
    
    function $ajax(url,method,data,callback) {
        xhr = new XMLHttpRequest();  // XMLHttpRequest 对象
        xhr.open(method, url, true); //post方式,url为服务器请求地址,true 该参数规定请求是否异步处理。
        xhr.responseType = "";//接收的数据类型
        xhr.onload = function () {
            if ((this.status >= 200 && this.status < 300) || this.status == 304) {
                callback(this.response);
            }
        }; //请求完成
        xhr.send(data); //开始上传,发送form数据
    }

    .net后台代码,接收文件后并保存在c:/wsz/pic/ 文件夹下

    public void ProcessRequest(HttpContext context)
            {
                HttpFileCollection hfc = context.Request.Files;
    
                for (var i = 0; i < hfc.Count; i++) {
                    HttpPostedFile hpf = hfc[i];
    
                    //文件保存目录路径
                    String savePath = "c:/wsz/pic/";
    
                    String filePath = savePath + hpf.FileName;
                    hpf.SaveAs(filePath);
                }
                
            }
  • 相关阅读:
    第十章 泛型程序设计与C++标准模板库 迭代器
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
  • 原文地址:https://www.cnblogs.com/wsz168/p/8409247.html
Copyright © 2011-2022 走看看