我使用jQuery的ajax函数将文件发送到服务器端PHP脚本时遇到问题.可以获取文件列表$('#fileinput').attr('files')但是如何将此数据发送到服务器?使用文件输入时$_POST,服务器端php脚本上的结果array()为0(NULL).
我知道这是可能的(虽然我到目前为止还没有找到任何jQuery解决方案,只有Prototye代码(http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html) ).
这似乎是相对较新的,所以请不要通过XHR/Ajax提及文件上传,因为它肯定有用.
我需要Safari 5中的功能,FF和Chrome会很好但不是必需的.
我现在的代码是:
$.ajax({
url: 'php/upload.php',
data: $('#file').attr('files'),
cache: false,
contentType: 'multipart/form-data',
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud) 我已经实现了这个脚本用于上传带有ajax的文件,它在浏览器以外的其他浏览器中工作得很完美,我注意到IE9不支持formData,更少,IE中的formData有什么替代品,我想用干净的javascript
function doObjUploadExplorer(url, lnk_id, file, progress, success, content, frm, div_dlg, start_func){
var file_input = null,
frm_data = new FormData(),
req;
try {
//firefox, chrome, safari etc
req = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer Browsers
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (document.getElementById(file)) {
file_input = document.getElementById(file);
for (var i = 0; i < file_input.files.length; ++i) {
frm_data.append(file, file_input.files[i]);
}
}
req.upload.addEventListener('progress', function(e) { //Event called while upload is in progress
if (progress !== undefined
&& e.lengthComputable) { …Run Code Online (Sandbox Code Playgroud)