ajax如何 实现 文件上传

ajax如何 实现 文件上传,第1张

程序说明

使用说明

实例化时,第一个必要参数是file控件对象:

new QuickUpload(file)

第二个可选参数用来设置系统的默认属性,包括

属性:    默认值//说明

parameter: {},//参数对象

action:  "",//设置action

timeout: 0,//设置超时(秒为单位)

onReady: function(){},//上传准备时执行

onFinish: function(){},//上传完成时执行

onStop:  function(){},//上传停止时执行

onTimeout: function(){}//上传超时时执行

还提供了以下方法:

upload:执行上传 *** 作;

stop:停止上传 *** 作;

dispose:销毁程序。

var QuickUpload = function(file, options) {

this.file = $$(file)

this._sending = false//是否正在上传

this._timer = null//定时器

this._iframe = null//iframe对象

this._form = null//form对象

this._inputs = {}//input对象

this._fFINISH = null//完成执行函数

$$.extend(this, this._setOptions(options))

}

QuickUpload._counter = 1

QuickUpload.prototype = {

//设置默认属性

_setOptions: function(options) {

this.options = {//默认值

action:        "",//设置action

timeout:    0,//设置超时(秒为单位)

parameter:    {},//参数对象

onReady:    function(){},//上传准备时执行

onFinish:    function(){},//上传完成时执行

onStop:        function(){},//上传停止时执行

onTimeout:    function(){}//上传超时时执行

}

return $$.extend(this.options, options || {})

},

//上传文件

upload: function() {

//停止上一次上传

this.stop()

//没有文件返回

if ( !this.file || !this.file.value ) return

//可能在onReady中修改相关属性所以放前面

this.onReady()

//设置iframe,form和表单控件

this._setIframe()

this._setForm()

this._setInput()

//设置超时

if ( this.timeout > 0 ) {

this._timer = setTimeout( $$F.bind(this._timeout, this), this.timeout * 1000 )

}

//开始上传

this._form.submit()

this._sending = true

},

//设置iframe

_setIframe: function() {

if ( !this._iframe ) {

//创建iframe

var iframename = "QUICKUPLOAD_" + QuickUpload._counter++,

iframe = document.createElement( $$B.ie ? "<iframe name=\"" + iframename + "\">" : "iframe")

iframe.name = iframename

iframe.style.display = "none"

//记录完成程序方便移除

var finish = this._fFINISH = $$F.bind(this._finish, this)

//iframe加载完后执行完成程序

if ( $$B.ie ) {

iframe.attachEvent( "onload", finish )

} else {

iframe.onload = $$B.opera ? function(){ this.onload = finish } : finish

}

//插入body

var body = document.body body.insertBefore( iframe, body.childNodes[0] )

this._iframe = iframe

}

},

//设置form

_setForm: function() {

if ( !this._form ) {

var form = document.createElement('form'), file = this.file

//设置属性

$$.extend(form, {

target: this._iframe.name, method: "post", encoding: "multipart/form-data"

})

//设置样式

$$D.setStyle(form, {

padding: 0, margin: 0, border: 0,

backgroundColor: "transparent", display: "inline"

})

//提交前去掉form

file.form && $$E.addEvent(file.form, "submit", $$F.bind(this.dispose, this))

//插入form

file.parentNode.insertBefore(form, file).appendChild(file)

this._form = form

}

//action可能会修改

this._form.action = this.action

},

//设置input

_setInput: function() {

var form = this._form, oldInputs = this._inputs, newInputs = {}, name

//设置input

for ( name in this.parameter ) {

var input = form[name]

if ( !input ) {

//如果没有对应input新建一个

input = document.createElement("input")

input.name = name input.type = "hidden"

form.appendChild(input)

}

input.value = this.parameter[name]

//记录当前input

newInputs[name] = input

//删除已有记录

delete oldInputs[name]

}

//移除无用input

for ( name in oldInputs ) { form.removeChild( oldInputs[name] ) }

//保存当前input

this._inputs = newInputs

},

//停止上传

stop: function() {

if ( this._sending ) {

this._sending = false

clearTimeout(this._timer)

//重置iframe

if ( $$B.opera ) {//opera通过设置src会有问题

this._removeIframe()

} else {

this._iframe.src = ""

}

this.onStop()

}

},

//销毁程序

dispose: function() {

this._sending = false

clearTimeout(this._timer)

//清除iframe

if ( $$B.firefox ) {

setTimeout($$F.bind(this._removeIframe, this), 0)

} else {

this._removeIframe()

}

//清除form

this._removeForm()

//清除dom关联

this._inputs = this._fFINISH = this.file = null

},

//清除iframe

_removeIframe: function() {

if ( this._iframe ) {

var iframe = this._iframe

$$B.ie ? iframe.detachEvent( "onload", this._fFINISH ) : ( iframe.onload = null )

document.body.removeChild(iframe) this._iframe = null

}

},

//清除form

_removeForm: function() {

if ( this._form ) {

var form = this._form, parent = form.parentNode

if ( parent ) {

parent.insertBefore(this.file, form) parent.removeChild(form)

}

this._form = this._inputs = null

}

},

//超时函数

_timeout: function() {

if ( this._sending ) { this._sending = false this.stop() this.onTimeout() }

},

//完成函数

_finish: function() {

if ( this._sending ) { this._sending = false this.onFinish(this._iframe) }

}

}

方法和详细的 *** 作步骤如下:

1、第一步,安装并配置PHP集成环境,以phpstudy为例,见下图,转到下面的步骤。

2、第二步,完成上述步骤后,在www目录中创建一个新的html文件(new1.html)和一个php(ab.php)文件,见下图,转到下面的步骤。

3、第三步,  完成上述步骤后,html文件首先引用所需的类库jquery文件,然后引用正文部分,以简单的表单提交为例,见下图,转到下面的步骤。

4、第四步,完成上述步骤后,html的javascript-ajax部分,以上一步正文中的cilik事件为例,见下图,转到下面的步骤。

5、第五步,完成上述步骤后,php文件的准备部分主要使用$ _POST接受ajax传输的数据,并且可以返回不同类型的数据。

 例如回声结果或转码为json格式的json_encode数据将返回到前端进行处理,见下图,转到下面的步骤。

6、第六步,完成上述步骤后,结果显示:提交表单中的内容后,无需刷新即可显示该内容,见下图。这样,就解决了这个问题了。

ajax() 方法通过 HTTP 请求加载远程数据。该方法是 jQuery 底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。$.ajax() 返回其创建的 XMLHttpRequest 对象。大多数情况下你无需直接 *** 作该函数,除非你需要 *** 作不常用的选项,以获得更多的灵活性。

工具原料:编辑器、浏览器

1、使用ajax改变文本的一个简单的代码实例如下:

<html>

<head>

<script type="text/javascript" src="/jquery/jquery.js"></script>

<script type="text/javascript">

$(document).ready(function(){

  $("#b01").click(function(){

  htmlobj=$.ajax({url:"/jquery/test1.txt",async:false})

  $("#myDiv").html(htmlobj.responseText)

  })

})

</script>

</head>

<body>

<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>

<button id="b01" type="button">改变内容</button>

</body>

</html>

2、运行的结果如下图:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11860700.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存