.NET中RadAsyncUpload临时文件删除问题:存库后删/自动失效
Great questions—let's tackle them one by one to get your temp file cleanup working reliably:
1. Does Telerik provide a built-in method to delete temporary files?
Absolutely! Telerik includes native methods to clean up temp files as soon as you're done processing them (like saving their content to your database).
When working with RadAsyncUpload, each uploaded file is wrapped in an UploadedFile object. After you've saved the file's content to your database, you can call the Delete() method directly on that object to remove the corresponding temp file instantly:
foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles) { // Save the file content to your database (use file.InputStream to access the data) SaveFileToDatabase(file.InputStream, file.FileName); // Delete the temp file right after saving to the database file.Delete(); }
If you need to delete a temp file later (by its filename), you can use the static RadAsyncUpload.DeleteTempFile() method:
string tempFileServerPath = Server.MapPath("~/App_Data/RadUploadTemp/your-temp-filename.tmp"); RadAsyncUpload.DeleteTempFile(tempFileServerPath);
2. What to do if the auto-delete mechanism fails, and manual deletion is tricky due to dynamic filenames?
First, let's figure out why the auto-delete might not be working:
- Check your web.config settings: The default 40-minute auto-cleanup relies on the
radUploadConfigurationsection. Make sure it's configured correctly (you can adjust the intervals if needed):<configuration> <configSections> <sectionGroup name="telerik.web.ui"> <section name="radUploadConfiguration" type="Telerik.Web.UI.RadUploadConfigurationSection, Telerik.Web.UI" requirePermission="false"/> </sectionGroup> </configSections> <telerik.web.ui> <radUploadConfiguration cleanUpInterval="40" maxFileAge="40" /> </telerik.web.ui> </configuration> - Check application pool recycling: If your app pool recycles before the cleanup task runs, the auto-delete won't trigger. Ensure your pool's recycling schedule gives the cleanup enough time to run.
If auto-delete still isn't working, here are two solid manual solutions:
a. Delete temp files immediately after processing (using UploadedFile properties)
Every UploadedFile has a TempFileName property that gives you the full server path to the temp file. You can use this to delete the file directly if the Delete() method isn't an option:
foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles) { // Save file content to database SaveFileToDatabase(file.InputStream, file.FileName); // Get the full path to the temp file string fullTempFilePath = file.TempFileName; // Delete the file if it exists if (System.IO.File.Exists(fullTempFilePath)) { System.IO.File.Delete(fullTempFilePath); } }
b. Create a custom scheduled cleanup task
To handle orphaned temp files (those that weren't deleted during processing), you can build a background task that scans the RadUploadTemp directory and deletes files older than your threshold (e.g., 40 minutes).
In ASP.NET, you can use HostingEnvironment.RegisterObject to run this task periodically:
public class TempFileCleanupService : IRegisteredObject { private readonly Timer _cleanupTimer; private readonly string _tempDirectoryPath; private readonly TimeSpan _maxFileAge = TimeSpan.FromMinutes(40); public TempFileCleanupService(string tempDirPath) { _tempDirectoryPath = tempDirPath; // Run cleanup immediately, then every 40 minutes _cleanupTimer = new Timer(PerformCleanup, null, TimeSpan.Zero, TimeSpan.FromMinutes(40)); } private void PerformCleanup(object state) { try { var tempDir = new DirectoryInfo(_tempDirectoryPath); foreach (var file in tempDir.GetFiles()) { if (DateTime.Now - file.CreationTime > _maxFileAge) { file.Delete(); } } } catch (Exception ex) { // Log the error (avoid crashing the app) System.Diagnostics.Trace.WriteLine($"Temp file cleanup failed: {ex.Message}"); } } public void Stop(bool immediate) { _cleanupTimer?.Dispose(); HostingEnvironment.UnregisterObject(this); } } // Register the service when your app starts (e.g., in Global.asax.cs) protected void Application_Start(object sender, EventArgs e) { string tempDirPath = Server.MapPath("~/App_Data/RadUploadTemp"); HostingEnvironment.RegisterObject(new TempFileCleanupService(tempDirPath)); }
This ensures that even if some temp files slip through the cracks during processing, they'll get cleaned up automatically on a regular schedule.
内容的提问来源于stack exchange,提问作者Ritanshu Barnwal

