I think this is pretty simple. My program will create an Excel file on the server using Automation. After that, I want to force the user to choose whether or not to download it, then, regardless of the choice, delete the file so it won't just take up space. Any ideas on how I could accomplish this?
You could read the file into an input buffer then delete the file and send the buffer to the user instead of the actual file.
It's hard to know when the user has actually finished downloading the file, and you don't want to delete it before doing so, therefore my advice is to delete files older than a certain amount of time, for example > 24 hours.
It's hard to determine if the user has finished downloading the file because the browser doesn't transmit this information, and other than packet sniffing the network data and configuring in detail your webserver to manage its open connections, and actually believing it won't make errors, there's no simple way.
It also depends how you're reading the file and its actual size, for example going line by line to output to the browser will save memory, but it'll make the code dependant on the file's existence, while reading the file's content to some kind of buffer will allow the webserver to well, serve it, but depending on the number of visitors, the file size, and average download speed, you might have at any time a webserver with dozens of GB files in memory.
It's just easier to have enough space for a few days worth of data and delete files every now and then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With