gpt4 book ai didi

php - Ajax 图像上传,其中图像获取添加到文件名的日期时间戳,并通过 MYSQL 插入图像标题

转载 作者:行者123 更新时间:2023-11-29 13:08:06 26 4
gpt4 key购买 nike

是否有任何 AJAX PHP Jquery MySQL 插件可以上传多个文件(最好是拖放),将日期时间戳添加到实际的文件名(它存储在服务器上的文件夹中)。此外,您还可以选择插入 MySQL 数据库的图 block 和描述(mysql 行将具有如下列:USER_ID、IMAGE_FILENAME、TITLE、DESCRIPTION)。我正在尝试为我的基于用户的网站的用户创建图片数据库。

http://hayageek.com/drag-and-drop-file-upload-jquery/

我尝试过对其进行正确的修改,但无法使其正常工作...我喜欢这个设计,但功能还不够。有什么想法或建议可以满足我的问题吗?

<小时/>
<h1>Source Javascript</h1>

function sendFileToServer(formData,status)
{
var uploadURL ="<? echo $dyn_www; ?>/php_parsers/upload.php"; //Upload URL
var extraData ={}; //Extra Data.
var jqXHR=$.ajax({
xhr: function() {
var xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
//Set progress
status.setProgress(percent);
}, false);
}
return xhrobj;
},
url: uploadURL,
type: "POST",
contentType:false,
processData: false,
cache: false,
data: formData,
success: function(data){
status.setProgress(100);

$("#status1").append("");
}
});

status.setAbort(jqXHR);
}
var rowCount=0;
function createStatusbar(obj)
{
rowCount++;
var row="odd";
var filename = this.filename;
if(rowCount %2 ==0) row ="even";

this.statusbar = $("<div class='statusbar "+row+"'></div>");
this.filename = $("<div class='filename'></div>").appendTo(this.statusbar);
this.size = $("<div class='filesize'></div>").appendTo(this.statusbar);
this.progressBar = $("<div class='progressBar'><div></div></div>").appendTo(this.statusbar);
this.abort = $("<div class='abort'>Abort</div>").appendTo(this.statusbar);
this.success = $("<i style=\"color:#009900;display:none\" class=\"fa fa-check-circle\"></i>").appendTo(this.statusbar);
this.titleOfImage = $("<div class='title'><input name='image_file_name' value='"+file_date+"' /><button id=\"title_button_"+row+"\" type=\"submit\"><i class=\"fa fa-chevron-circle-right\"></i></div></div>").appendTo(this.statusbar);
obj.after(this.statusbar);

this.setFileNameSize = function(name,size)
{
var sizeStr="";
var sizeKB = size/1024;
if(parseInt(sizeKB) > 1024)
{
var sizeMB = sizeKB/1024;
sizeStr = sizeMB.toFixed(2)+" MB";
}
else
{
sizeStr = sizeKB.toFixed(2)+" KB";
}

this.filename.html(name);
this.size.html(sizeStr);
}
this.setProgress = function(progress)
{
var progressBarWidth =progress*this.progressBar.width()/ 100;
this.progressBar.find('div').animate({ width: progressBarWidth }, 10).html(progress + "% ");
if(parseInt(progress) >= 100)
{
this.abort.hide();
this.success.show();
this.titleOfImage.show();
}
}
this.setAbort = function(jqxhr)
{
var sb = this.statusbar;
this.abort.click(function()
{
jqxhr.abort();
sb.hide();
});
}
}
function handleFileUpload(files,obj)
{
for (var i = 0; i < files.length; i++)
{


var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var fulldatetime = ""+ curr_date + "-" + curr_month + "-" + curr_year +"";



var filename = files[i].name;
var ext = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
var allowed= "";
if(ext == "jpg" || ext == "png" || ext == "gif" || ext == "jpeg")
{
var fd = new FormData();
fd.append('file', files[i]);
var status = new createStatusbar(obj); //Using this we can set progress.
status.setFileNameSize(files[i].name,files[i].size);
sendFileToServer(fd,status);
}else
{
alert("You are only allowed to upload .jpg, .png, & .gif");
}
} //end for
}


$(document).ready(function()
{
var obj = $("#dragandrophandler");
obj.on('dragenter', function (e)
{
e.stopPropagation();
e.preventDefault();
$(this).css('border', '2px solid #0B85A1');
});
obj.on('dragover', function (e)
{
e.stopPropagation();
e.preventDefault();
});
obj.on('drop', function (e)
{

$(this).css('border', '2px dotted #0B85A1');
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
//We need to send dropped files to Server
handleFileUpload(files,obj);
});
$(document).on('dragenter', function (e)
{
e.stopPropagation();
e.preventDefault();
});
$(document).on('dragover', function (e)
{
e.stopPropagation();
e.preventDefault();
obj.css('border', '2px dotted #0B85A1');
});
$(document).on('drop', function (e)
{
e.stopPropagation();
e.preventDefault();
});

});
</script>

最佳答案

与您的代码一起尝试:

替换此 block :

/*
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var fulldatetime = ""+ curr_date + "-" + curr_month + "-" + curr_year +"";
*/

与:

 var d = new Date();
var fulldatetime = d.getDate() + "-"
+ (d.getMonth()+1) + "-"
+ d.getFullYear() + "--"
+ d.getHours() + "-"
+ d.getMinutes() + "-"
+ d.getSeconds();

然后:

var filename = files[i].name;
var ext = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
/* this line added */
var file_date = fulldatetime+'_'+filename;
var allowed= "";
if(ext == "jpg" || ext == "png" || ext == "gif" || ext == "jpeg")
{
var fd = new FormData();
fd.append('file', files[i]);
/* this line added */
fd.append('fileDateTime',file_date);
var status = new createStatusbar(obj); //Using this we can set progress.

其余代码保持不变。您可以在 upload.php

上使用 $_POST['fileDateTime']; 来寻址新文件名

添加:

要将重命名的文件传递给 createStatusbar(),请更改函数定义:

来自:

/* function createStatusbar(obj) */

至:

function createStatusbar(obj,file_date)

在调用期间:

/* var status = new createStatusbar(obj);  //Using this we can set progress.*/

至:

var status = new createStatusbar(obj,file_date); //Using this we can set progress.

file_date 是保存新文件名的变量,您可以根据需要使用它。

关于php - Ajax 图像上传,其中图像获取添加到文件名的日期时间戳,并通过 MYSQL 插入图像标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22433129/

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