文件在被取消之前上传

我在这里尝试取消文件上传的一些情况。 我所做的是,如果用户点击“取消”按钮,那么它将只删除iframe,使其不会转到将文件上传到服务器并将数据插入数据库的页面。

现在这可以正常工作,如果用户在快速时间点击“取消”按钮,我已经意识到的问题是,如果用户很晚点击“取消”按钮,它有时不会及时删除iframe意味着刚刚在用户点击“取消”按钮之前上传了该文件。

所以我的问题是,如果文件在用户点击“取消”按钮之前以某种方式上传,它会删除数据库中的数据并从服务器中删除文件吗?

以下是图片上传表格:

Loading...




下面是控制“取消”按钮的jquery函数:

 $(imageuploadform).find(".imageCancel").on("click", function(event) { $('.upload_target_image').get(0).contentwindow $("iframe[name='upload_target_image']").attr("src", "javascript:''"); return stopImageUpload(2); }); 

下面是php代码,它上传文件并将数据插入数据库。 上面的表单发布到这个php页面“imageupload.php”:

  prepare($imagesql)) { // Handle errors with prepare operation here } //bind the parameters (these are the values that will be inserted) $insert->bind_param("s",$img); //Assign the variable of the name of the file uploaded $img = 'ImageFiles/'.$_FILES['fileImage']['name']; //execute INSERT query $insert->execute(); if ($insert->errno) { // Handle query error here } //close INSERT query $insert->close(); //Retrieve the ImageId of the last uploded file $lastID = $mysqli->insert_id; //Insert into Image_Question Table (be using last retrieved Image id in order to do this) $imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId) VALUES (?, ?, ?)"; //prepare the above SQL statement if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { // Handle errors with prepare operation here echo "Prepare statement err imagequestion"; } //Retrieve the question number $qnum = (int)$_POST['numimage']; //bind the parameters (these are the values that will be inserted) $insertimagequestion->bind_param("isi",$lastID, 'Exam', $qnum); //execute INSERT query $insertimagequestion->execute(); if ($insertimagequestion->errno) { // Handle query error here } //close INSERT query $insertimagequestion->close(); ?>   window.top.stopImageUpload(, '');   

更新:

下面是php代码“cancelimage.php”,我想从服务器中删除已取消的文件,并从数据库中删除记录。 它已设置但尚未完成,有人可以完成它,所以我可以使用$ _SESSION检索文件的名称和它的ID吗?

 prepare($imagedeletesql)) { // Handle errors with prepare operation here } //Dont pass data directly to bind_param store it in a variable $delete->bind_param("s",$img); //execute DELETE query $delete->execute(); if ($delete->errno) { // Handle query error here } //close query $delete->close(); ?> 

您能否在答案中提供示例代码,以便我更轻松。 谢谢

好吧,就像我之前的回答中所说,你可以使用$_SESSION存储$lastID$ImageFile

例如,在$lastID = $mysqli->insert_id; 插入以下内容:

 $_SESSION['lastID'] = $lastID; $_SESSION['ImageFile'] = $_FILES["fileImage"]["name"]; 

这将在会话中存储lastIDImageFile

cancelimage.php您输入以下内容:

 unlink("ImageFiles/" . $_SESSION['ImageFile']); $delete = $mysqli->prepare('DELETE FROM Image WHERE id = ?'); $delete->bind_param("i",$_SESSION['lastID']); $delete->execute(); 

现在向取消按钮添加一个动作,将iframe的源更新为cancelimage.php

但存在风险,因为如果用户之前已上传文件,则上传新文件但文件未通过(因此会话变量未设置),则将删除先前的文件。

好了,因为您使用PDO,您可以使用:

 $insert->rollBack(); 

回滚最近的插入。 并找到上传的文件并使用:

 unlink('test.html'); 

有关rollBackunlink更多信息,请查看这些链接

  • 取消链接
  • 回滚

如果iframe被删除并且没有调用告诉上传哪个文件的回调,那么很难知道要删除哪个文件。

虽然您可以在$lastID存储上次上传文件的$lastID$ImageFile ,并请求在数据库中查找文件的其他页面,但删除该记录,然后从磁盘中删除该文件。

我可以提供一些示例代码,但我的建议是您修改当前代码,使其更具可读性并修复缩进。

另一种解决方案是使用以下函数显式中止上载:

 function abortUpload(){ var iframeObj = $("iframe[name='upload_target_image']").get(0); if (iframeObj.contentWindow.document.execCommand){ // IE iframeObj.contentWindow.document.execCommand('Stop'); }else{ // other browsers iframeObj.contentWindow.stop(); } }