I am facing a problem with Image caching by using files in ASP.NET. My detailed solution is:
For 1st request: I check the image file not exists and write the image file into hard disk. After that returns the url of the file to the browser
For later requests: the function just returns the url of the file to the browser
My problem is: sometimes with 1st requests, the browser can not get the image data from the url and cause the 404 - File Not Found error. I think it might because of the file is written but not ready for retrieving from browsers, but in the code I am using both "using statement" and "Dispose method" for bitmap & graphic objects to ensure everything are ready for accessing from browsers.
In more details, I am using the similar solution as in NopCommerce:
lock (s_lock) { ...........
using (var stream = new MemoryStream(LoadPictureBinary(picture))) {
using(var b = new Bitmap(stream)){
using(var newBitMap = new Bitmap(newSize.Width, newSize.Height)){
.......................
newBitMap.Save(
Path.Combine(this.LocalThumbImagePath, localFilename), ici, ep);
}
}
}
} // end lock
return url;
More information: you can check the full code at http://nopcommerce.codeplex.com/SourceControl/changeset/view/9125b9a7fafe#src%2fLibraries%2fNop.Services%2fMedia%2fPictureService.cs (please search the GetPictureUrl() function)