zoukankan      html  css  js  c++  java
  • formdata 上传图片+进度条

    记得引入jquery

     

        //上传进度回调函数:  
            function progressHandlingFunction(e) {
                if (e.lengthComputable) {
                    $('progress').attr({ value: e.loaded, max: e.total }); //更新数据到进度条  
                    var percent = e.loaded / e.total * 100;
                    $('#progress').html(e.loaded + "/" + e.total + " bytes. " + percent.toFixed(2) + "%");
                }
            }


                $("#upFilebtn").click(function () {
                    //判断文件类型
                    var file = $("#AjaxFileData")[0].files[0];
                    var imgType = (file.name.substr(file.name.lastIndexOf("."))).toLowerCase();
                    if (imgType != ".jpg" && imgType != ".gif" && imgType != ".jpeg" && imgType != ".png") {
                        alert("您上传图片的类型不符合(.jpg|.jpeg|.gif|.png)!");
                        return false;
                    }
                    if (($("#AjaxFileData")[0].files[0].size / 1024) > (5 * 1024)) {
                        alert("上传图片不得大于 5M");
                        return;
                    }
                    var formData = new FormData();
                    formData.append("username", "formName");
                    formData.append("file", file);
                    $.ajax({
                        url: '喂喂改一下',
                        type: 'post',
                        data: formData,
                        xhr: function () { //获取ajaxSettings中的xhr对象,为它的upload属性绑定progress事件的处理函数  
                            myXhr = $.ajaxSettings.xhr();
                            if (myXhr.upload) { //检查upload属性是否存在  
                                //绑定progress事件的回调函数  
                                myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
                            }
                            return myXhr; //xhr对象返回给jQuery使用  
                        },
                        processData: false,
                        contentType: false,
                        success: function (msg) {
                       
                        alert('不谈了');
                        }
                    });
                });

    html:

      <input id="AjaxFileData" type="file" >

      <input  type="button" id="upFilebtn" >

    上传进度:<progress></progress><br/>  
        <p id="progress">0 bytes</p>  
        <p id="info"></p>

    server:

      string fileDir = HttpContext.Current.Server.MapPath("~/uploadfile/Do");

           HttpPostedFile file = context.Request.Files["file"];

        file.SaveAs(Path.Combine(fileDir, fileName));

    好人甲 : https://www.cnblogs.com/tyqing/p/5995538.html

  • 相关阅读:
    Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题
    java JDK JRE 1.6,1.7,1.8各个版本版本下载链接
    window 10 企业版激活
    IntelliJ IDEA 缺少 javax 包 支持
    IntelliJ Idea 2017 免费激活方法
    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】
    【转载】 CUDA_DEVICE_ORDER 环境变量说明 ( ---------- tensorflow环境下的应用 )
    【转载】 TensorFlow tf.app&tf.app.flags用法介绍
    中国知网(cnki)上caj格式转pdf的方法 ----------------- 转载
    同时购入两台同款thinkpad笔记本电脑,分别使用同一账户激活office失败--------------解决方法(账户下有多个Office激活信息,重装后提示“许可证不正确或者最大激活次数”)
  • 原文地址:https://www.cnblogs.com/j2ee-web-01/p/9269539.html
Copyright © 2011-2022 走看看