仅使用Javascript将$ _FILES和$ _POST合并到一个AJAX调用中

时间:2016-02-08 16:56:17

标签: javascript php ajax

我在发送包含文件和字符串的ajax调用时遇到困难。我可以毫无困难地制作帖子或档案,但不能同时制作。我需要在纯Javascript中使用它,我比Jquery更精通。
这是我正在使用的代码......

    function uploadFile(){
var title = _('title').value;
var genere = _('genere').value;
var stars = _('stars').value;
var description = _('description').value;
    var file = _("video").files[0];
     //alert(file.name+" | "+file.size+" | "+file.type);
    var formdata =  new FormData();
formdata.append("video", file);
formdata.append("title", title);
formdata.append("genere", genere);
formdata.append("stars", stars);
formdata.append("description", description);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "video_php/video_upload.php");
    ajax.send(formdata);
}
function progressHandler(event){
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
}

这是php .....

<?PHP
$fileName = '';
$fileTmpLoc = '';
$fileType = '';
$fileSize = '';
$title = '';
$genere = '';
$stars = '';
$description = '';
$retn=  '';
if (!isset($_FILES["video"]["name"])) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}else{
    $fileName       =   $_FILES["video"]["name"]; // The file name
$fileTmpLoc     =   $_FILES["video"]["tmp_name"]; // File in the PHP tmp folder
$fileType       =   explode('.',$fileName); // The type of file it is
$fileType       =   end($fileType); // The end type of file it is
$fileSize       =   $_FILES["video"]["size"]; // File size in bytes
$title = preg_replace('#[^a-z0-9, ]#i', '', $_POST['title']);
$genere = preg_replace('#[^a-z0-9, ]#i', '', $_POST['genere']);
$stars = preg_replace('#[^a-z0-9, ]#i', '', $_POST['stars']);
$description = preg_replace('#[^a-z0-9, ]#i', '', $_POST['description']);
}
echo $fileName.'<br/>';
echo $fileTmpLoc.'<br/>';
echo $fileType.'<br/>';
echo $fileSize.'<br/>';
echo $title.'<br/>';
echo $genere.'<br/>';
echo $description.'<br/>';
?>

我已经坚持了几个星期。我已经通过谷歌以及许多其他网站与谷歌。关于我想要的结果的每个答案都在Jquery中。我有一个解决方案是制作一个两步形式,上传第一个数据集,然后上传另一个,但我认为两者都可以一起发送以便用户使用。我在其他几个网站上看过它 注* 以上代码已更正。添加了

formdata.append("video", file);
formdata.append("title", title);
formdata.append("genere", genere);
formdata.append("stars", stars);
formdata.append("description", description);

由于@Ohgodwhy

,它现在已经完全正常运行了 原始问题在Send $_POST and $_FILE data to php with JAVASCRIPT

1 个答案:

答案 0 :(得分:1)

您希望将_('title')的值添加到formdata

鉴于你有这个:

formdata.append("video", file);

您需要做的就是:

formdata.append('title', title);

FormData对象将处理文件的传输,title将以$_POST['title'];

的形式提供