HTML 5 文件拖拽通过php删除文件上传
好吧,这里有点厚 - 我想,我正在看 pangratz / dnd-file-upload 并拖动 & drop 很棒等(我知道不能跨浏览器等,但并不重要),但我不能做的是创建正确的 php 代码来处理实际的上传。
这是基本的js代码
$(document).ready(function(){
$.fn.dropzone.uploadStarted = function(fileIndex, file){
var infoDiv = $("<div></div>");
infoDiv.attr("id", "dropzone-info" + fileIndex);
infoDiv.html("upload started: " + file.fileName);
var progressDiv = $("<div></div>");
progressDiv.css({
'background-color': 'orange',
'height': '20px',
'width': '0%'
});
progressDiv.attr("id", "dropzone-speed" + fileIndex);
var fileDiv = $("<div></div>");
fileDiv.addClass("dropzone-info");
fileDiv.css({
'border' : 'thin solid black',
'margin' : '5px'
});
fileDiv.append(infoDiv);
fileDiv.append(progressDiv);
$("#dropzone-info").after(fileDiv);
};
$.fn.dropzone.uploadFinished = function(fileIndex, file, duration){
$("#dropzone-info" + fileIndex).html("upload finished: " + file.fileName + " ("+getReadableFileSizeString(file.fileSize)+") in " + (getReadableDurationString(duration)));
$("#dropzone-speed" + fileIndex).css({
'width': '100%',
'background-color': 'green'
});
};
$.fn.dropzone.fileUploadProgressUpdated = function(fileIndex, file, newProgress){
$("#dropzone-speed" + fileIndex).css("width", newProgress + "%");
};
$.fn.dropzone.fileUploadSpeedUpdated = function(fileIndex, file, KBperSecond){
var dive = $("#dropzone-speed" + fileIndex);
dive.html( getReadableSpeedString(KBperSecond) );
};
$.fn.dropzone.newFilesDropped = function(){
$(".dropzone-info").remove();
};
$("#dropzone").dropzone({
url : "upload.php",
printLogs : true,
uploadRateRefreshTime : 500,
numConcurrentUploads : 2
});
});
,但我无法让upload.php以任何方式工作
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = '';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
建议等。欢迎我所需要做的就是将图像上传到服务器并保存到数据库(一旦我可以拖动&保存就不错了) ; 删除图像以移动到服务器。 哦,我不想使用第三方应用程序
。
OK, being a bit thick here - I think, am looking at pangratz / dnd-file-upload and the drag & drop is great etc (I know not cross browser etc but does not matter) but what I cannot do is create the right php code to process the actual upload.
Here is the base js code
$(document).ready(function(){
$.fn.dropzone.uploadStarted = function(fileIndex, file){
var infoDiv = $("<div></div>");
infoDiv.attr("id", "dropzone-info" + fileIndex);
infoDiv.html("upload started: " + file.fileName);
var progressDiv = $("<div></div>");
progressDiv.css({
'background-color': 'orange',
'height': '20px',
'width': '0%'
});
progressDiv.attr("id", "dropzone-speed" + fileIndex);
var fileDiv = $("<div></div>");
fileDiv.addClass("dropzone-info");
fileDiv.css({
'border' : 'thin solid black',
'margin' : '5px'
});
fileDiv.append(infoDiv);
fileDiv.append(progressDiv);
$("#dropzone-info").after(fileDiv);
};
$.fn.dropzone.uploadFinished = function(fileIndex, file, duration){
$("#dropzone-info" + fileIndex).html("upload finished: " + file.fileName + " ("+getReadableFileSizeString(file.fileSize)+") in " + (getReadableDurationString(duration)));
$("#dropzone-speed" + fileIndex).css({
'width': '100%',
'background-color': 'green'
});
};
$.fn.dropzone.fileUploadProgressUpdated = function(fileIndex, file, newProgress){
$("#dropzone-speed" + fileIndex).css("width", newProgress + "%");
};
$.fn.dropzone.fileUploadSpeedUpdated = function(fileIndex, file, KBperSecond){
var dive = $("#dropzone-speed" + fileIndex);
dive.html( getReadableSpeedString(KBperSecond) );
};
$.fn.dropzone.newFilesDropped = function(){
$(".dropzone-info").remove();
};
$("#dropzone").dropzone({
url : "upload.php",
printLogs : true,
uploadRateRefreshTime : 500,
numConcurrentUploads : 2
});
});
But I cannot get upload.php to work in any way
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = '';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
Suggestions etc. welcome all I need to do is upload the inmage to the server and sane to a DB (saving not bad ONCE I can get the drag & dropped image to move to the server. Oh Idpn't want to use a third party app.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有找到如何用你的拖拽来做到这一点。删除库。因为我认为ajax请求没有设置$_FILES。
我知道这不是一个完整的解决方案,但如果您确实需要快速答案,这里是:
jquery-filedrop
使用此库,您可以发送设置“paramname”的 POST 变量。
有了这个,你的 php 代码应该可以工作了。我已经用过它了,非常简单。
希望有帮助。
I don't find yet how to do it with your drag & drop library. Because I think ajax request don't set $_FILES.
I know it's not a complete solution but if you really need a quick answer, here it is :
jquery-filedrop
With this library you can send POST variables setting 'paramname'.
with this, your php code should work. I have already used it, it's pretty simple.
hope it helps.