gpt4 book ai didi

javascript - 提交时将 div 图像上传到数据库

转载 作者:行者123 更新时间:2023-11-28 05:20:22 25 4
gpt4 key购买 nike

我一直在尝试获得有关如何上传从 div 创建的图像的答案。我有创建图像 div 的代码,现在我需要知道如何将该图像上传到我的数据库文件夹,我需要在提交时使用唯一的文件名上传它。最让我担心的是如何在提交时上传。是否可以使用与上传常规图像文件相同的代码上传 div 的图像?

$(document).ready(function(){


var element = $("#firstshirt"); // global variable
var getCanvas; // global variable

$("#btn-Preview-Image").on('click', function () {
html2canvas(element, {
onrendered: function (canvas) {
$("#previewImage").append(canvas);
getCanvas = canvas;
}
});
});

$("#btn-Convert-Html2Image").on('click', function () {
var imgageData = getCanvas.toDataURL("image/png");
// Now browser starts downloading it instead of just showing it
var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
$("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
});

});
<center><input id="btn-Preview-Image" type="button" value="Preview"/>
<button type="button" id="btn-Convert-Html2Image" href="#">Download</button>
<br/>

</center>
<center><input type="submit" name="submit" value="Next" /></center>
这是我上传的图片 php
<?php
require_once("configur.php");


$query='UPDATE profile_table SET images="'.$_FILES['file']['name'].'"
WHERE email= "'.$_SESSION['email'].'"';

if ($mysqli->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$mysqli->close();
?>


<?php

include('configur.php');
if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.



if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";



}
else
{
// check if file already exit in "images" folder.
if (file_exists("upload/" . $_FILES["file"]["name"]))
{

}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into profile_table";

if(mysqli_query($link, $query_image))
{
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
else
{
echo'';
}
}
}




}
}


?>

最佳答案

“insert into profile_table” 是无效的 MySql(或者至少不会改变任何东西),我会移动
$query='UPDATE profile_table SET images="'.$_FILES['file']['name'].'"
WHERE email= "'.$_SESSION['email'].'"';
最后在文件已上传成功子句下并将其路径存储在数据库中(将 images="'.$_FILES['file']['name'].'" 更改为 images="'上传/.$_FILES['文件']['名称'].'"').要访问该文件,您现在可以查询数据库并通过另一个 php 脚本返回流。

关于javascript - 提交时将 div 图像上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com