我正试图通过FilesAPI上传多张图片。这里有以下代码:
$(document).ready(function()
{
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
uploadFile(f);
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function uploadFile(file)
{
var xhr = new XMLHttpRequest();
var formData = new FormData();
formData.append('file',file);
xhr.open('POST', 'upload_team.php');
xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
xhr.send(formData);
$('#test').load('upload_team.php');
}
});我如何“传输”图像信息,到PHP脚本上传?我试过这样做:
<?php
echo $_FILES['file'];
if(isset($_FILES['file'])) {
echo "hej<br>";
}
?>但是没有执行回显内部的代码。
有人能帮我吗?
发布于 2014-02-09 22:50:37
http://jsfiddle.net/Tarnum/rya6X/7/ (我稍微修改了函数uploadFile )
PHP的后端
<?php
if(isset($_FILES['file'])) {
echo $_FILES['file']['name'];
}
?>http://www.php.net/manual/en/features.file-upload.post-method.php
应该管用的。
如果没有--尝试以通常的方式发送表单,而不使用JavaScript和AJAX。例如,从http://jsfiddle.net/复制代码,修复属性"action",选择文件并单击submit按钮。
对于服务器,这将与使用AJAX的HTTP请求完全相同。
如果问题还在继续,那么问题就在服务器端的某个地方。可能在服务器配置中禁用了上传文件的能力。
发布于 2014-02-09 22:06:20
首先,html "form“元素应该具有以下属性:
enctype= multipart/form-data剩下的答案在这里,处理文件上载/Php手册
https://stackoverflow.com/questions/21665584
复制相似问题