zoukankan      html  css  js  c++  java
  • js上传文件工具类


    个人博客 地址:http://www.wenhaofan.com/article/20180808210417

    jQuery.extend({

    uploadUtil:function(){

    }

    });


    /**

     * 上传file form

     */

    $.uploadUtil.prototype.uploadFileForm=function(paras){

      if(this.uploadServerUrl==undefined){

    throw "upload.js->uploadServerUrl not defined"

      }

     

      $.ajax({

          url:this.uploadServerUrl,     //上传图片请求的路径

          method:'POST',            //方法

          data:paras.dataForm,                 //数据

          processData: false,        //告诉jQuery不要加工数据

          dataType:'json',

          contentType: false,   

          success: function(data) {

          paras.success(data);

          },

          error:function(data){

          if(paras.erro|| typeof paras.error  === "function"){

      paras.erro(data);

          }

          }

      });

    }

    /**

     * 上传js file文件

     */

    $.uploadUtil.prototype.uploadFile= function (paras) {

    var dataForm=new FormData();

    var file=paras.file;

    var resultFile;

    //如果是jquery对象 且类型为input file 则抛出异常

    if(this.isJquery(file)&&this.isFileInput(file)){

    resultFile=$(file).get(0).files[0];

    }else{

    resultFile=file;

    }

    dataForm.append('upfile',paras.file);

    paras.dataForm=dataForm;

    this.uploadFileForm(paras);

    }

    /**

     * 判断是否是file input

     */

    $.uploadUtil.prototype.isFileInput=function(obj){

    var tagName=$(obj)[0].tagName;

    return tagName=="INPUT"&&obj.attr("type").toLowerCase=="file";

    }

    /**

     * 判断是否是jquery对象

     */

    $.uploadUtil.prototype.isJquery=function(obj){

    return obj instanceof jQuery;

    }


    $.uploadUtil.prototype.uploadServerUrl=undefined;

    /**

     * 设置上传路径

     */

    $.uploadUtil.prototype.setUploadServerUrl=function(uploadServerUrl){

    this.uploadServerUrl=uploadServerUrl;

    }


    /**

     * 获取上传路径

     */

    $.uploadUtil.prototype.getUploadServerUrl=function(){

    return this.uploadServerUrl;

    }

  • 相关阅读:
    日志框架 log4j2 全解析
    SpringMVC开发RESTful接口
    SpringMVC 进阶
    SpringMVC 参数映射与文件上传
    SSM整合
    算法分析
    SpringMVC 入门
    数据结构与算法概念
    在普通WEB项目中使用Spring
    《算法导论》——重复元素的随机化快排Optimization For RandomizedQuickSort
  • 原文地址:https://www.cnblogs.com/fanwenhao/p/9637382.html
Copyright © 2011-2022 走看看