I have a asp.net mvc5 website, that gets some image files from a api and stores them in a image folder.
public async Task<bool> getPhoto64(UserTest user)
{
try
{
if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
using (var client = new HttpClient())
{
string baseUrl = ConfigurationManager.AppSettings["BaseUrl"];
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync("user/image?userIid=" + user.IID);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
Stream st = await response.Content.ReadAsStreamAsync();
byte[] bytes = new byte[(int)st.Length];
st.Read(bytes, 0, (int)st.Length);
if (bytes != null)
{
if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
File.WriteAllBytes(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png")), bytes);
}
}
}
}
catch (Exception e)
{
logger.Error(e);
return false;
}
return true;
}
Right now im doing this when a user connects to the website, but i would like to know if there is a way for the website to automatically trigger this method , lets say once a day at 00:00.