文件之前,他们被取消上载到 [英] files get uploaded just before they get cancelled

查看:109
本文介绍了文件之前,他们被取消上载到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个小这里的情况,我正在尝试取消文件的上传。我所做的是说,如果用户点击取消按钮,然后它会简单地删除了iframe,以便它不会进入页面,其上载文件到服务器和数据插入到数据库中。

Got a little situation here where I am trying to cancel a file's upload. What I have done is stated that if the user clicks on the "Cancel" button, then it will simply remove the iframe so that it does not go to the page where it uploads the files into the server and inserts data into the database.

现在这个工作正常,如果用户点击quickish时间取消按钮,我已经意识到虽然问题是,如果用户点击取消按钮很晚,有时可能不会在删除的iframe时间这意味着该文件刚刚上传用户单击取消按钮。

Now this works fine if the user clicks on the "Cancel" button in quickish time the problem I have realised though is that if the user clicks on the "Cancel" button very late, it sometimes doesn't remove the iframe in time meaning that the file has just been uploaded just before the user has clicked on the "Cancel" button.

所以我的问题是,有没有办法,如果文件不以某种方式获取用户点击之前上载于取消按钮,它会删除数据库中的数据,并从服务器中删除该文件?

So my question is that is there a way that if the file does somehow get uploaded before the user clicks on the "Cancel" button, that it deletes the data in the database and removes the file from the server?

下面是图片上传方式:

<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的功能,它控制了取消按钮:

Below is the jquery function which controls the "Cancel" button:

$(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的code在那里上载的文件和将数据插入到数据库中。以上职位这个PHP页面imageupload.php的形式:

Below is the php code where it uploads the files and inserts the data into the database. The form above posts to this php page "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的codecancelimage.php在这里我想从服务器上删除已取消的文件,并从数据库中删除记录。它设置了,但还没完,有人可以完成它,所以我可以检索该文件的名称和它的ID使用$ _SESSION?

Below is the php code "cancelimage.php" where I want to delete the cancelled file from the server and delete the record from the database. It is set up but not finished, can somebody finish it off so I can retrieve the name of the file and it's id using $_SESSION?

<?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();

?>

您可以请提供样品code在你的答案使我更容易。谢谢

Can you please provide an sample code in your answer to make it easier for me. Thank you

推荐答案

好吧,在我的previous答案就像说,你可以使用 $ _ SESSION 存储 $ lastID $镜像文件

Alright, like said in my previous answer, you could use the $_SESSION to store $lastID and $ImageFile.

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

这将存储 lastID 镜像文件会话。

unlink("ImageFiles/" . $_SESSION['ImageFile']); 

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

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

但是存在这样的风险,因为如果用户之前已经上载的文件,上传一个新的,但该文件没有打通(所以会话瓦尔未设置),则previous文件将被删除

Now add an action to the cancel button that updates the source of the iframe to cancelimage.php

But there is a risk, because if the user has uploaded a file before, uploads a new one but the file didn't get through (so the session vars are not set) then the previous file will be deleted.

这篇关于文件之前,他们被取消上载到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆