jQuery Ajax在图像上传方面的进展
jQuery Ajax progress on image upload
我使用Ajax请求在服务器上上传图像,我使用这些代码,它很有效,但它只返回100%,而不会返回任何其他百分比。我该怎么办?
$('#uploadImgForm').change(function(e){
var th = $(this);
$.ajax({
url: "post/imageInsert.php",
type: "POST",
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
beforeSend: function(){
$("#imgBack").html('uploading').show();
},
xhr: function()
{
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = Math.floor(evt.loaded / evt.total) * 100 + '%';
$('#progress').css({width : percentComplete});
}
}, false);
return xhr;
},
success: function(data){
if(!data['error']) $("#imgBack").html($('<img>').attr('src',data['success']));
else $("#imgBack").html(data['error']);
},
error: function(){
$("#imgBack").html('Error').hide();
}
});
});
试试这个代码
$('#uploadImgForm').change(function(e){
var th = $(this);
$.ajax({
url: "post/imageInsert.php",
type: "POST",
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
beforeSend: function(){
$("#imgBack").html('uploading').show();
},
xhrFields: {
onprogress: function (evt) {
if (evt.lengthComputable) {
var percentComplete = Math.floor(evt.loaded / evt.total) * 100 + '%';
$('#progress').css({width : percentComplete});
}
}
},
success: function(data){
if(!data['error'])
$("#imgBack").html($('<img>').attr('src',data['success']));
else
$("#imgBack").html(data['error']);
},
error: function(){
$("#imgBack").html('Error').hide();
}
});
});
我最近也做了同样的事情(制作一个进度条来指示图像上传完成的百分比),下面的代码可以工作;一定要试试:
$("#uploadImgForm").on("submit", upload_image);
function update(evt){
var percentComplete = Math.floor(evt.loaded / evt.total) * 100 + '%';
$('#progress').css({width : percentComplete});
}
function upload_image(event){
event = event || window.event;
// Prevent the default form action i.e. loading of a new page
if(event.preventDefault){ // W3C Variant
event.preventDefault();
}
else{ // IE < 9
event.returnValue = false;
}
$.ajax({
url: "post/imageInsert.php",
type: "POST",
data: new FormData($('#uploadImgForm')[0]),
beforeSend: function(){
$("#imgBack").html('uploading').show();
},
xhr: function(){
// get the native XmlHttpRequest object
var xhr = $.ajaxSettings.xhr() ;
// set the onprogress event handler
xhr.upload.onprogress = update,
// set the onload event handler
xhr.upload.onload = function(){ } ;
// return the customized object
return xhr ;
},
success: function(data){
if(!data['error']) $("#imgBack").html($('<img>').attr('src',data['success']));
else $("#imgBack").html(data['error']);
},
error: function(){
$("#imgBack").html('Error').hide();
},
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false
});
}
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Ajax发布表单序列化,发布引号'
- 通过Ajax将JavaScript函数传递给PHP文件
- ajax请求的顺序总是不同的
- 可以't使用Polymer's的核心ajax
- Ajax Live搜索发布到Laravel视图
- Ajax聊天消息重复而不仅仅是更新
- 从控制器返回后Ajax启动事件激发
- 在使用javascript的Ajax方面遇到了困难
- 在jQuery上发出ajax请求后,闭包在获取变量值方面似乎迟到了
- 为什么 AJAX 在部分数据序列化方面失败
- 并行 ajax 请求还是 sql 查询?[性能方面]
- 性能方面:拥有许多小型 ajax php 控制器脚本还是一个大型脚本更好
- jQuery Ajax在图像上传方面的进展
- PHP, Javascript, Ajax和获取结果的方面
- 简单的AJAX web应用程序示例,包括客户端和服务器方面