小编典典

文件在取消之前就已上传

ajax

我在这里尝试取消文件上传时遇到了一些情况。我所做的事情是说,如果用户单击“取消”按钮,则它将仅删除iframe,这样它就不会进入将文件上传到服务器并将数据插入数据库的页面。

现在,如果用户快速单击“取消”按钮,则可以正常工作,但我意识到的问题是,如果用户单击时间很晚,则有时无法及时删除iframe该文件刚刚在用户点击“取消”按钮之前上传。

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

以下是图片上传表单:

<form action="imageupload.php" method="post" enctype="multipart/form-data" target="upload_target_image" onsubmit="return imageClickHandler(this);" class="imageuploadform" >
  <p class="imagef1_upload_process" align="center">
    Loading...<br/>
    <img src="Images/loader.gif" />
  </p>
  <p class="imagef1_upload_form" align="center">
    <br/>
    <span class="imagemsg"></span>
    <label>Image File: <input name="fileImage" type="file" class="fileImage" /></label><br/>
    <br/>
    <label class="imagelbl"><input type="submit" name="submitImageBtn" class="sbtnimage" value="Upload" /></label>
  </p>
  <p class="imagef1_cancel" align="center">
    <input type="reset" name="imageCancel" class="imageCancel" value="Cancel" />
  </p>
  <iframe class="upload_target_image" name="upload_target_image" src="#" style="width:0px;height:0px;border:0px;solid;#fff;"></iframe>
</form>

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

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

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

<body>
<?php

include('connect.php');

session_start();

$result = 0;


//uploads file
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;

//set up the INSERT SQL query command to insert the name of the image file into the "Image" Table
$imagesql = "INSERT INTO Image (ImageFile) 
VALUES (?)";

//prepare the above SQL statement
if (!$insert = $mysqli->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();

?>

<!--Javascript which will output the message depending on the status of the upload (successful, failed or cancelled)-->

<script>
    window.top.stopImageUpload(<?php echo $result; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
</script>
</body>

更新:

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

<?php

// connect to the database
include('connect.php');

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}


//remove file from server
unlink("ImageFiles/...."); //need to retrieve file name here where the ... line is

//DELETE query statement where it will delete cancelled file from both Image and Image Question Table
$imagedeletesql = " DELETE img, img_q 
FROM Image AS img 
LEFT JOIN Image_Question AS img_q 
ON img_q.ImageId = img.ImageId 
WHERE img.ImageFile = ?";

//prepare delete query
if (!$delete = $mysqli->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();

?>

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


阅读 228

收藏
2020-07-26

共1个答案

小编典典

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

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

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

这将存储lastID并存储ImageFile在会话中。

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

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

2020-07-26