javascript - Dropzone.js 文件未将文件上传到服务器
问题描述
编辑:我还想补充一点,无论是 js 还是 PHP,我都没有收到任何错误。
从技术上讲,我正在使用一个表格,我希望卖家填写产品的必要内容,还包括要在购物区显示的产品图片。我讨厌html5的默认输入类型文件,我很难隐藏它,所以我遇到了dropzone。现在 dropzone 在客户端有点工作,但似乎在服务器端没有工作。没有文件上传到我希望发送的重定向上。
这对我来说是一种新事物,所以我不知道该怎么做。
这是html代码
<form enctype='multipart/form-data' action='upload.php' method='POST'>
<div class='form-group'>
<input class='form-control' type='text' placeholder='Title' id='titleInput' name='titleInput'>
</div>
<div class='form-group'>
<input class='form-control' type='number' placeholder='Price' id='priceInput' name='priceInput'>
</div>
<div class='dropzone' id='myDropzone'>
</div>
<button id='submit-all' type='submit' name='Post'>Submit<\button>
</form>
javascript代码
Dropzone.options.myDropzone= {
url: 'upload.php',
paramName: "file",
autoProcessQueue: false,
uploadMultiple: false,
//parallelUploads: 5,
//maxFiles: 5,
maxFilesize: 1,
acceptedFiles: 'image/*',
addRemoveLinks: true,
init: function() {
dzClosure = this; // Makes sure that 'this' is understood inside the functions below.
// for Dropzone to process the queue (instead of default form behavior):
document.getElementById("submit-all").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
//e.preventDefault();
e.stopPropagation();
dzClosure.processQueue();
});
//send all the form data along with the files:
this.on("sendingmultiple", function(data, xhr, formData) {
formData.append("titleInput", jQuery("#titleInput").val());
formData.append("priceInput", jQuery("#priceInput").val());
});
}
}
和upload.php代码
<?php
if(isset($_POST['Post'])){
if(isset($_FILES['file']['name'])){
$imageName = $_FILES['file']['name'];
$imageNameTemp = $_FILES['file']['tmp_name'];
echo $imageName;
}
else{
$imageName = "";
echo "null";
}
$errorMessage = "";
if($imageName != "") {
$targetDir = "assets/images/productImages/";
$imageName = $targetDir . uniqid() . basename($imageName);
$imageFileType = pathinfo($imageName, PATHINFO_EXTENSION);
if($_FILES['file']['size'] > 5000000) {
$errorMessage = "Sorry your file is too big!";
$uploadOk = 0;
}
if(strtolower($imageFileType) != "jpeg" && strtolower($imageFileType) != "png" && strtolower($imageFileType) != "jpg") {
$errorMessage = "Sorry, only jpeg, jpg and png files are allowed";
$uploadOk = 0;
}
if(strtolower($imageFileType) == "jpeg" || "jpg"){
$src = imagecreatefromjpeg($imageNameTemp);
}
if(strtolower($imageFileType) == "png"){
$src = imagecreatefrompng($imageNameTemp);
}
if($uploadOk == 1) {
list($width_min, $height_min) = getimagesize($imageNameTemp);
if($width_min > 1920){
$newwidth_min = 1920;
$newheight_min = ($height_min / $width_min) * $newwidth_min;
}
else if($height_min > 1920){
$newheight_min = 1920;
$newwidth_min = ($height_min / $width_min) * $newheight_min;
}
else{
$newheight_min = $height_min;
$newwidth_min = $width_min;
}
$tmp_min = imagecreatetruecolor($newwidth_min, $newheight_min);
imagecopyresampled($tmp_min, $src, 0, 0, 0, 0, $newwidth_min, $newheight_min, $width_min, $height_min);
imagejpeg($tmp_min, $imageName);
$uploadOk = 1;
}
else {
//image did not upload
$uploadOk = 0;
}
}
}
?>
因此,当它重定向到upload.php 时,它会回显null,因为如果没有发送文件,那是我写的。
请帮忙
解决方案
推荐阅读
- c++ - 从Linux源代码生成Windows可执行文件?
- c++ - 如何将 QList 与 std::generate 一起使用?
- python - 如何显式检查numba的cfunc的签名?
- java - 在 Java 中查询弹性搜索
- mongodb - 在mongo中对组进行子排序?
- javascript - 在 p5.js 中创建实体 3D 形状
- django - 将 Amazon S3 连接到现有的 Django 项目
- python - 如何将多行文本文件分解为一个可以迭代的列表以检查 python 中的输入 == 文本文件?
- python - 自定义加入条件。Left Join on ID 和 Nearest 小于 date。SQL 或 Python 解决方案
- apache-pig - 在 T:tuple 中找不到字段