PDA

View Full Version : Delete file after cancelled download



David Wellman
01-16-2010, 09:47 AM
Hi folks,

As part of my install I allow the user to download a file.

It is possible that the user (being a user !) will cancel the download before it has finished.

What I would like to do in this situation is to delete the downloaded file (ie the portion of the file that has reached the user's computer).

I tried putting a simple File.Delete command in the "On cancel" event but that fails with a 1024 error. (1024 = "Failed to delete one or more files").

I'm guessing that the file in question is still 'locked' by the SUF8 run-time but can't be certain as I can't get those details (whinge, whinge !).

Does anyone have any ideas how to do this ?

Cheers,
Dave

pww
01-16-2010, 11:00 AM
How do you download? With HTTP.Download or in another way?
Does the downloaded part exist after installer exits?

I guess file is deleted automatically when Cancel is confirmed. You can check that by monitoring the target folder. Or the downloaded part may not be stored in the target folder but somewhere else, and moved to target after download completes.

David Wellman
01-16-2010, 11:59 AM
Hi PWW,

Sorry, I should have said that I'm using HTTP.Download.

Yes, the downloaded file (or part of it) still exists after the installer has exited. I've waited for a number of minutes on a Windows pc which is not doing anything else, so if there was an async task to delete the file it should have gone by now. It hasn't (so i don't think it will do automatically).

Watching the downoad target folder you can see the download file growing in size as the download progresses, so it is not being out anywhere else first.

Even if I downloaded the file to somewhere else first, I'll still have the problem of deleting the file if the user cancels (this is not happening automatically).

Cheers,
Dave

jassing
01-16-2010, 12:23 PM
do you handle the user pressing 'cancel'? it sounds like the download isn't actually canceled.
You can also use File.DeleteOnReboot()

pww
01-16-2010, 12:30 PM
then I would download it to _TempLaunchFolder and move it to where I want after the download has finished. If it's cancelled, part will remain inside system's temp folder.

David Wellman
01-19-2010, 11:01 AM
Hi,

I have tried downloading to _TempLaunchFolder but the same principle applies.

If the user cancels then the partial download is left inside this folder.

Jassing - yes I do handle the 'cancel' request in that I have code in the "on cancel" event which asks "are you sure" and that code also deletes the download file - which doesn't work, giving a 1024 error. It's very possible that the download is not properly cancelled. How do I do that ? I can't see an "HTTP.Cancel" action.

Cheers,
Dave

jassing
01-19-2010, 11:02 AM
post your code.

David Wellman
01-19-2010, 11:10 AM
Here ya go...

==== handling the cancel event ===
The code in the ON-CANCEL event from my download screen;
local lsPROC = "WARD DownloadUpdate ONCANCEL";
local lnErrorCode = 0;

-- from _SUF70_Global_Functions.lua:
-- ask user if they're sure they want to exit
if waConfirmUserCancel(SessionVar.Expand("%WindowTitle%" , lsPROC)) then

if File.DoesExist(SessionVar.Expand("%SWUDownloadFilePath%")) then
File.Delete(SessionVar.Expand("%SWUDownloadFilePath%"), false, false, false, nil);
lnErrorCode = Application.GetLastError();
if (lnErrorCode ~= 0) then -- did we activate other copy
SetupData.WriteToLogFile(lsPROC..": Error number "..lnErrorCode.." trying to delete final download file.\r\n",true);
end -- did we activate other copy
end

if File.DoesExist(_TempLaunchFolder.."\\"..tbFileName.Filename..tbFileName.Extension) then
File.Delete(_TempLaunchFolder.."\\"..tbFileName.Filename..tbFileName.Extension, false, false, false, nil);
lnErrorCode = Application.GetLastError();
if (lnErrorCode ~= 0) then -- did we activate other copy
SetupData.WriteToLogFile(lsPROC..": Error number "..lnErrorCode.." trying to delete temporary download file.\r\n",true);
end -- did we activate other copy
end

Application.Exit(EXIT_REASON_USER_ABORTED);
end

==== my own function to check if user really means to cancel ====
function waConfirmUserCancel(asTitle, asCaller)
local lsPROC = "WARD waConfirmUserCancel";
local nResult = Dialog.Message(asTitle, "The Software Updates processing is not complete.\r\nDo you really want to cancel?", MB_YESNO, MB_ICONQUESTION, MB_DEFBUTTON2);
local lStr = "NO";
if (nResult == IDYES) then
lStr = "YES";
end
SetupData.WriteToLogFile(lsPROC..": User choice from Cancel request is "..lStr..".\r\n",true);
return (nResult == IDYES);
end


==== download command ====
My download command (in the ON-START event for the same screen) is;

HTTP.Download(SessionVar.Expand("%SWUDownloadURL%") ,_TempLaunchFolder.."\\"..tbFileName.Filename..tbFileName.Extension , MODE_BINARY, 20, 80, nil, nil, DownloadCallback);

There is other code in this event but this is the download command itself.

Cheers,
Dave

jassing
01-19-2010, 11:14 AM
Post your project file instead.

David Wellman
01-19-2010, 11:36 AM
Hi Jassing,

Brilliant, that worked. Thanks very much. I hadn't appeciated the need to cancel from inside the callback function, but it's now running 'sweetly'.

Cheers,
Dave