当前位置: 首页 > 图文教程 > 网络编程 > Javascript > jQuery Ajax文件上传(php)

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 jQuery Ajax文件上传(php)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 422 ::
收藏到网摘: n/a

今我在网上找了好久哟。。。才找到JQUERY方式的异步文件上传,赶紧记了下来。 实际上在用PHP上传文件时。。。只能用$_FILES形式,但是若我们只是单一的用JS方式取其ID,如<input id='img' type='file'>..document.getElementById('img').value或者jquery形式的$("#img")都是不能正真实际上传的(但是还是有很多人这样做,刚开始时我也是)。
可是功能上又要要求实现所谓的“异步上传”,怎么办呢??只能借助于第三方的组件,或者自己写一个。但如果是考虑开发时间,那以用第三方的,这里有一个不错的jQuery的Ajax文件上传的组件,是“ajaxfileupload.js",其组件下载地址为:http://www.phpletter.com/,下载完毕里面有一个php的应用demo,很容易看懂的。
过程:
首先引入JQUERY的主文件:
复制代码 代码如下:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
­
再写一个传值文件
­
<script type="text/javascript">
function ajaxFileUpload()
{
$.ajaxFileUpload
(
{
[url='/cms/manage/newsajax/newsadd']url:'/cms/manage/newsajax/newsadd'[/url],
secureuri:false,
fileElementId:'img',
dataType: 'text',
success: function (data)
{
alert(data);
}
}
)
return false;
}
</script>

­
这样客户端就完成了。
再服务器端时,为了简便检测是否真正的传值过来了,你可以打印$_FILES.
­
var_dump($_FILES);
­
这样你又看到你熟悉的数组了:
­
复制代码 代码如下:

array(
'name'=>'lamp.jpg',
'type'=>'image/pjpeg',
'tmp_name'=>'c:\windows\temp\phpFA.tmp',
'error'=>0,
'size'=>3127
)

。。。就这么简单 。。。。有问题的朋友可以联系我哟。。。QQ:944597216,(这个博客我不经常上)