php ajax 文件上传实现代码(1/2)

一款完整ajax 文件上传功能,我们利用了jquery ajax上传插件来实现的,下面代码非常完整。

<html>
<head>
<style>
body{font-size:12px; bgcolor:menu;}
table{font-size:12px;}
</style>
<title></title>
<meta http-equiv="content-stype" content="text/html; charset=gb2312" />
<script type="text/网页特效" src="http://code.google.com/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" >

jquery.extend({
 

    createuploadiframe: function(id, uri)
 {
   //create frame
            var frameid = 'juploadframe' + id;
           
            if(window.activexobject) {
                var io = document.createelement('<iframe id="' + frameid + '" name="' + frameid + '" />');
                if(typeof uri== 'boolean'){
                    io.src = 'javascript:false';
                }
                else if(typeof uri== 'string'){
                    io.src = uri;
                }
            }
            else {
                var io = document.createelement('iframe');
                io.id = frameid;
                io.name = frameid;
            }
            io.style.position = 'absolute';
            io.style.top = '-1000px';
            io.style.left = '-1000px';

            document.body.appendchild(io);

            return io   
    },
    createuploadform: function(id, fileelementid)
 {
  //create form 
  var formid = 'juploadform' + id;
  var fileid = 'juploadfile' + id;
  var form = $('<form  action="" method="post" name="' + formid + '" id="' + formid + '" enctype="multipart/form-data"></form>'); 
  var oldelement = $('#' + fileelementid);
  var newelement = $(oldelement).clone();
  $(oldelement).attr('id', fileid);
  $(oldelement).before(newelement);
  $(oldelement).appendto(form);
  //set attributes
  $(form).css教程('position', 'absolute');
  $(form).css('top', '-1200px');
  $(form).css('left', '-1200px');
  $(form).appendto('body');  
  return form;
    },

    ajaxfileupload: function(s) {
        // todo introduce global settings, allowing the client to modify them for all requests, not only timeout  
        s = jquery.extend({}, jquery.ajaxsettings, s);
        var id = new date().gettime()       
  var form = jquery.createuploadform(id, s.fileelementid);
  var io = jquery.createuploadiframe(id, s.secureuri);
  var frameid = 'juploadframe' + id;
  var formid = 'juploadform' + id;  
        // watch for a new set of requests
        if ( s.global && ! jquery.active++ )
  {
   jquery.event.trigger( "ajaxstart" );
  }           
        var requestdone = false;
        // create the request object
        var xml = {}  
        if ( s.global )
            jquery.event.trigger("ajaxsend", [xml, s]);
        // wait for a response to come back
        var uploadcallback = function(istimeout)
  {   
   var io = document.getelementbyid(frameid);
            try
   {    
    if(io.contentwindow)
    {
      xml.responsetext = io.contentwindow.document.body?io.contentwindow.document.body.innerhtml:null;
                  xml.responsexml = io.contentwindow.document.xmldocument?io.contentwindow.document.xmldocument:io.contentwindow.document;
     
    }else if(io.contentdocument)
    {
      xml.responsetext = io.contentdocument.document.body?io.contentdocument.document.body.innerhtml:null;
                 xml.responsexml = io.contentdocument.document.xmldocument?io.contentdocument.document.xmldocument:io.contentdocument.document;
    }      
            }catch(e)
   {
    jquery.handleerror(s, xml, null, e);
   }
            if ( xml || istimeout == "timeout")
   {    
                requestdone = true;
                var status;
                try {
                    status = istimeout != "timeout" ? "success" : "error";
                    // make sure that the request was successful or notmodified
                    if ( status != "error" )
     {
                        // process the data (runs the xml through httpdata regardless of callback)
                        var data = jquery.uploadhttpdata( xml, s.datatype );   
                        // if a local callback was specified, fire it and pass it the data
                        if ( s.success )
                            s.success( data, status );
   
                        // fire the global callback
                        if( s.global )
                            jquery.event.trigger( "ajaxsuccess", [xml, s] );
                    } else
                        jquery.handleerror(s, xml, status);
                } catch(e)
    {
                    status = "error";
                    jquery.handleerror(s, xml, status, e);
                }

                // the request was completed
                if( s.global )
                    jquery.event.trigger( "ajaxcomplete", [xml, s] );

                // handle the global ajax counter
                if ( s.global && ! --jquery.active )
                    jquery.event.trigger( "ajaxstop" );

                // process result
                if ( s.complete )
                    s.complete(xml, status);

                jquery(io).unbind()

                settimeout(function()
         { try
          {
           $(io).remove();
           $(form).remove(); 
           
          } catch(e)
          {
           jquery.handleerror(s, xml, null, e);
          }         

         }, 100)

                xml = null

            }
        }
        // timeout checker
        if ( s.timeout > 0 )
  {
            settimeout(function(){
                // check to see if the request is still happening
                if( !requestdone ) uploadcallback( "timeout" );
            }, s.timeout);
        }
        try
  {
           // var io = $('#' + frameid);
   var form = $('#' + formid);
   $(form).attr('action', s.url);
   $(form).attr('method', 'post');
   $(form).attr('target', frameid);
            if(form.encoding)
   {
                form.encoding = 'multipart/form-data';    
            }
            else
   {    
                form.enctype = 'multipart/form-data';
            }   
            $(form).submit();

        } catch(e)
  {   
            jquery.handleerror(s, xml, null, e);
        }
        if(window.attachevent){
            document.getelementbyid(frameid).attachevent('onload', uploadcallback);
        }
        else{
            document.getelementbyid(frameid).addeventlistener('load', uploadcallback, false);
        }   
        return {abort: function () {}}; 

    },

    uploadhttpdata: function( r, type ) {
        var data = !type;
        data = type == "xml" || data ? r.responsexml : r.responsetext;
        // if the type is "script", eval it in global context
        if ( type == "script" )
            jquery.globaleval( data );
        // get the javascript object, if json is used.
        if ( type == "json" )
            eval( "data = " + data );
        // evaluate scripts within html
        if ( type == "html" )
            jquery("<div>").html(data).evalscripts();
   //alert($('param', data).each(function(){alert($(this).attr('value'));}));
        return data;
    }
})

</script>
<script type="text/javascript">
 function ajaxfileupload()
 {
  $("#loading")
  .ajaxstart(function(){
   $(this).show();
  })
  .ajaxcomplete(function(){
   $(this).hide();
  });

  $.ajaxfileupload
  (
   {
    url:'do_ajax_file_upload.php教程',
    secureuri:false,
    fileelementid:'filetoupload',
    datatype: 'json',
    success: function (data, status)
    {
     if(typeof(data.error) != 'undefined')
     {
      if(data.error != '')
      {
       alert(data.error);
      }else
      {
                            window.opener.document.<?php echo $fromform;?>.<?php echo $fromedit;?>.value=data.msg;
                            alert('上传成功!');
                            window.close();
      }
     }
    },
    error: function (data, status, e)
    {
     alert(e);
    }
   }
  )
  
  return false;

 }
</script>

 

首页 1 2 末页

时间: 2024-09-04 07:47:20

php ajax 文件上传实现代码(1/2)的相关文章

jQuery+php ajax文件上传实现代码

很多项目中需要用到即时上传功能,比如,选择本地图片后,立即上传并显示图像.本文结合实例讲解如何使用jQuery和PHP实现Ajax即时上传文件的功能,用户只需选择本地图片确定后即实现上传,并显示上传进度条,上传完成后,显示图片信息. HTML 本示例基于jQuery以及相当出色的jquery.form插件,所以,先要载入jquery库和form插件.  代码如下 复制代码 <script type="text/javascript" src="jquery.min.js

php+ajax实现图片文件上传实例代码

方法一,利用jquery ajaxfileupload.js文件上传 其实就是实现无刷新式的文件上传.可采用IFRAME文件上传原理. 实际上在用PHP上传文件时...只能用$_FILES形式,但是若我们只是单一的用JS方式取其ID,如<input id='img' type='file'>..document.getElementById('img').value或者jquery形式的$("#img")都是不能正真实际上传的(但是还是有很多人这样做,刚开始时我也是). 可

php Ajax实现异步文件上传的代码

php教程 ajax实现异步文件上传的代码 1:取得file对象 2:读取2进制数据 3:模拟http请求,把数据发送出去(这里通常比较麻烦) 在forefox下使用 xmlhttprequest 对象的 sendasbinary 方法发送数据: 4:完美实现 遇到的问题 目前仅有 firefox 可以正确上传文件.(chrome也可以采google.gears上传) 对于从firefox和chrome下读取到的文件数据好像不一样(不知道是否是调试工具的原因) chrome以及其他高级浏览器没有

ajax文件上传,springMVC报空指针异常

问题描述 ajax文件上传,springMVC报空指针异常 我使用的是ajaxfileupload.js调试了半天,崩溃了,使用form的submit提交时没有问题的,可是无法实现异步刷新,求大神指点!!! 解决方案 麻烦把你的后台控制器代码和你前端代码贴一下,最好把出错的日志也贴一下.

File, FileReader 和 Ajax 文件上传实例分析(php)_javascript技巧

File FileReader 可以干什么? Ajax文件上传例子 FileReader 对象可以读取文件的 Base64编码数据(readAsDataURL),2进制字符串(readAsBinaryString),文本(readAsText)并且都是异步的. 对了,Email拖拽附件上传就可以利用 FileReader 配合 Ajax 完成. File 对象 File对象可以从 input[type=file].files 数组,和拖拽事件 event.dataTransfer.files 中

HTML5 Ajax文件上传进度条如何显示_AJAX相关

原本打算使用jquery插件进行异步文件上传,比如uploadfy但是需要额外的支持,也有人用iframe模仿异步上传机制,感觉都比较别扭.因为项目不考虑低版本浏览器,所以决定用html5实现.下面只是一个简单的demo,具体样式需要自己去做. 后台基于strut2进行文件处理,具体因项目而定.只是要注意设置文件大小的限制.  <constant name="struts.multipart.maxSize" value="52428800"/>这个配置

jQuery插件AjaxFileUpload实现ajax文件上传_jquery

本文实例为大家分享了AjaxFileUpload实现文件上传的具体代码,供大家参考,具体内容如下 jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传. 1.引入AjaxFileUpload插件相关的js 复制代码 代码如下: <script type="text/javascript" src="<%=basePath%>resources/js/

深入浅析AjaxFileUpload实现单个文件的 Ajax 文件上传库_AJAX相关

jQuery.AjaxFileUpload.js是一款jQuery插件,用于通过ajax上传文件. 语法: $.ajaxFileUpload([options]) options参数说明: 1.url 上传处理程序地址. 2,fileElementId 需要上传的文件域的ID,即<input type="file">的ID. 3,secureuri 是否启用安全提交,默认为false. 4,dataType 服务器返回的数据类型.可以为xml,script,json,htm

iframe实现Ajax文件上传效果示例_AJAX相关

复制代码 代码如下: <span style="font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 14px; line-height: 21px; text-align: left; background-color: rgb(255, 254, 247); ">avascript部分</span> 复制代码 代码如下: <!doctype