微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajaxFileUpload插件的使用 ------------------插件

这个插件只能上传,不能判断文件大小,类型,要自己写js判断,或后台判断

http://www.phpletter.com/Our-Projects/AjaxFileUpload/

可以直接对DEMO进行修改就行

下面是一个函数

//ajax上传头像图片
function ajaxFileUpload() {
var filePath = $("#filetoUpload").val();
if (!filePath || filePath == "" || filePath == null) {
$("#filetoUploadMsg").html("").html("<font color='Red'>请选择一个上传图片</font>");
return false;
}
//显示和隐藏加载图片
$("#loading").ajaxStart(function () {
$(this).show();
})

.ajaxComplete(function () {
$(this).hide();
});

//ajax上传头像图片
$.ajaxFileUpload
(
{
url: '/User/Upload',
secureuri: false,
fileElementId: 'filetoUpload',
dataType: 'json',//这个是后台返回的数据类型,可以是json,text,html,script
success: function (data,status) {
var error = data.err;
var filepath = data.msg;
if (error != "") {
asyncBox.tips(error,'error');//异步盒子插件提示
} else {
$("#viewImage").attr("src",filepath);
$("#settingLeftimage").attr("src",filepath);
asyncBox.tips('上传头像成功 !','success');
}
},
error: function (data,status,e) {
asyncBox.tips(e,'error');
}
}
)
return false;
}

下面是下载的一个文件,(别人写的)

jQuery插件AjaxFileUpload可以实现ajax文件上传

jQuery插件AjaxFileUpload可以实现ajax文件上传,该插件使用非常简单,首先了解一下正确使用AjaxFileUpload插件方法,然后再了解一些常见的错误信息和解决方法

使用说明

需要使用jQuery库文件 和AjaxFileUpload库文件

使用实例

http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js

一,包含文件部分

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

二,HTML部分

<img id="loading " src="loading.gif" style="display:none;">
<input id="filetoUpload " type="file" size="20" name="filetoUpload " class="input">
<button class="button" id="buttonUpload" onclick="return ajaxFileUpload ();">上传</button>

只需要三个元素,一个动态加载小图标、一个文件域和一个按钮
注意:使用AjaxFileUpload插件上传文件可不需要form,如下:

<form name="form" action="" method="POST" enctype="multipart/form-data">
……相关HTML代码……
</form>

因为AjaxFileUpload插件自动生成一个form提交表单。

对于file文件域ID和name,ajaxFileUpload插件fileElementId参数需要获取文件域ID,如果处理上传文件操作就需要知道文件域name,以便获取上传文件信息,这两者关系一定要清楚。

三,javascript部分

<script type="text/javascript">
function ajaxFileUpload (){
loading();//动态加载小图标
$.ajaxFileUpload ({
url :'upload.PHP',
secureuri :false,
fileElementId :'filetoUpload',
dataType : 'json',
success : function (data,status){
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},e){
alert(e);
}
})
return false;
}
function loading (){
$("#loading ").ajaxStart(function(){
$(this).show();
}).ajaxComplete(function(){
$(this).hide();
});
}
</script>

主要参数说明:
1,url表示处理文件上传操作的文件路径,可以测试URL是否能在浏览器中直接访问,如上:upload.PHP
2,fileElementId表示文件域ID,如上:filetoUpload
3,secureuri是否启用安全提交,认为false
4,dataType数据数据,一般选json,javascript的原生态
5,success提交成功后处理函数
6,error提交失败处理函数

上面有两个方法一个动态加载小图标提示函数loading()和ajaxFileUpload文件上传$.ajaxFileUpload()函数,这与我们使用jQuery.ajax()函数差不多,使用很简单,
这里我省略了PHP处理上传文件,使用jQuery插件 AjaxFileUpload实现ajax文件就这么简单。

同时我们需要了解相关的错误提示

1,SyntaxError: missing ; before statement错误
如果出现这个错误就需要检查url路径是否可以访问

2,SyntaxError: Syntax error错误
如果出现这个错误就需要检查处理提交操作的PHP文件是否存在语法错误

3,SyntaxError: invalid property id错误
如果出现这个错误就需要检查属性ID是否存在

4,SyntaxError: missing } in XML expression错误
如果出现这个错误就需要检查文件名称是否一致或不存在

5,其它自定义错误
大家可使用变量$error直接打印的方法检查各参数是否正确,比起上面这些无效的错误提示还是方便很多。

使用jQuery插件AjaxFileUpload实现无刷新上传文件非常实用,由于其简单易用,因些这个插件相比其它文件上传插件使用人数最多,非常值得推荐。

处理页面

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class web_ajax_FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;


//if (files[0].ContentLength > 5)
//{
// Response.Write("{");
// Response.Write("msg:'" + files[0].FileName + "',");
// Response.Write("error:'文件上传失败'");
// Response.Write("}");
//}
//else
//{
// Response.Write("{");
// Response.Write("msg:'没有文件上传',");
// Response.Write("error:'文件上传失败'");
// Response.Write("}");
//}
files[0].SaveAs("d:/adw.jpg");
Response.Write("{");
Response.Write("msg:'a',");
Response.Write("error:''");
Response.Write("}");

//Response.Write("{"); //Response.Write("msg:'ggg\n',"); //Response.Write("error:'aa\n'"); //Response.Write("}"); Response.End(); } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐