简体   繁体   中英

How can I delete a picture if the user is changing the current page?

I'm trying to build an upload pictures system: when an upload is done at 100%, with ajax the user can see a preview of the picture. Then finaly he can choose or not to save the picture. My issue is: in any case, if he chooses or not to save the picture, the picture will be already uploaded. what is the best way to delete the picture if the user is changing the current url or if he is closing the current page?

you can add the column to database and with flag you can can know the user save the uploaded picture or not . and you can delete all picture that are noe save.

You can first upload the picture to temporary folder, then, when user clicked on save move it to its target location, at this case, if user leaved the page, the picture was in temp folder that you can delete all files inside that folder daily...

Another option is to use HTML5 file system API to read the file and show up thumbnail before upload, so that you can show confirmation before upload it...

Try

Calling php ajax on window unload . Something like

$(window).unload( function () 
{ 

   $.ajax({ url: "update.php",
        data: {'name':'pathtofile'},
        type: 'POST'
        success: function(){
           alert("done");
        }});
} 
);

in update.php do

unlink($_REQUEST['name'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM