【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
我可以使用以下jQuery代码使用ajax请求的POST方法执行文件上传吗?
$.ajax({
type: "POST",
timeout: 50000,
url: url,
data: dataString,
success: function (data) {
alert('success');
return false;
}
});
如果有可能,我是否需要填写data
部分? 这是正确的方法吗? 我只将文件发布到服务器端。
我一直在搜索,但是我发现是一个插件,而在我的计划中我不想使用它。 至少目前是这样。
#1楼
通过ajax上传文件不再需要iframe。 我最近自己做了。 查看以下页面:
http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface
更新了答案并进行了清理。 使用getSize函数检查大小或使用getType函数检查类型。 添加了progressbar html和css代码。
var Upload = function (file) {
this.file = file;
};
Upload.prototype.getType = function() {
return this.file.type;
};
Upload.prototype.getSize = function() {
return this.file.size;
};
Upload.prototype.getName = function() {
return this.file.name;
};
Upload.prototype.doUpload = function () {
var that = this;
var formData = new FormData();
// add assoc key values, this will be posts values
formData.append("file", this.file, this.getName());
formData.append("upload_file", true);
$.ajax({
type: "POST",
url: "script",
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', that.progressHandling, false);
}
return myXhr;
},
success: function (data) {
// your callback here
},
error: function (error) {
// handle error
},
async: true,
data: formData,
cache: false,
contentType: false,
processData: false,
timeout: 60000
});
};
Upload.prototype.progressHandling = function (event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
var progress_bar_id = "#progress-wrp";
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
// update progressbars classes so it fits your code
$(progress_bar_id + " .progress-bar").css("width", +percent + "%");
$(progress_bar_id + " .status").text(percent + "%");
};
如何使用Upload类
//Change id to your id
$("#ingredient_file").on("change", function (e) {
var file = $(this)[0].files[0];
var upload = new Upload(file);
// maby check size or type here with upload.getSize() and upload.getType()
// execute upload
upload.doUpload();
});
Progressbar HTML代码
<div id="progress-wrp">
<div class="progress-bar"></div>
<div class="status">0%</div>
</div>
Progressbar CSS代码
#progress-wrp {
border: 1px solid #0099CC;
padding: 1px;
position: relative;
height: 30px;
border-radius: 3px;
margin: 10px;
text-align: left;
background: #fff;
box-shadow: inset 1px 3px 6px rgba(0, 0, 0, 0.12);
}
#progress-wrp .progress-bar {
height: 100%;
border-radius: 3px;
background-color: #f39ac7;
width: 0;
box-shadow: inset 1px 1px 10px rgba(0, 0, 0, 0.11);
}
#progress-wrp .status {
top: 3px;
left: 50%;
position: absolute;
display: inline-block;
color: #000000;
}
#2楼
如果您想这样做:
$.upload( form.action, new FormData( myForm))
.progress( function( progressEvent, upload) {
if( progressEvent.lengthComputable) {
var percent = Math.round( progressEvent.loaded * 100 / progressEvent.total) + '%';
if( upload) {
console.log( percent + ' uploaded');
} else {
console.log( percent + ' downloaded');
}
}
})
.done( function() {
console.log( 'Finished upload');
});
比
https://github.com/lgersman/jquery.orangevolt-ampere/blob/master/src/jquery.upload.js
可能是您的解决方案。
#3楼
$("#submit_car").click( function() {
var formData = new FormData($('#car_cost_form')[0]);
$.ajax({
url: 'car_costs.php',
data: formData,
async: false,
contentType: false,
processData: false,
cache: false,
type: 'POST',
success: function(data)
{
},
}) return false;
});
编辑:注意contentype和处理数据您可以简单地使用它通过Ajax上传文件……提交的输入不能在form元素之外:)
#4楼
这是我在想的一个主意:
Have an iframe on page and have a referencer.
具有将INPUT:File元素移动到的形式。
Form: A processing page AND a target of the FRAME.
结果将发布到框架,然后您可以将获取的数据向上一级发送到所需的图像标签,例如:
data:image/png;base64,asdfasdfasdfasdfa
并加载页面。
我相信它对我有用,并且取决于您是否可以执行以下操作:
.aftersubmit(function(){
stopPropigation()// or some other code which would prevent a refresh.
});
#5楼
您可以按以下方式使用ajaxSubmit方法:)选择需要上传到服务器的文件,然后将其提交到服务器:)
$(document).ready(function () {
var options = {
target: '#output', // target element(s) to be updated with server response
timeout: 30000,
error: function (jqXHR, textStatus) {
$('#output').html('have any error');
return false;
}
},
success: afterSuccess, // post-submit callback
resetForm: true
// reset the form after successful submit
};
$('#idOfInputFile').on('change', function () {
$('#idOfForm').ajaxSubmit(options);
// always return false to prevent standard browser submit and page navigation
return false;
});
});
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3145955