Reputation: 39
I am currently developing a web application in c# (ASP.NET MVC).
I need to upload images on this site, and right now I am saving the images in a folder locally, like this:
[HttpPost]
public ActionResult Create(Product product, HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
return View(product);
}
else
{
if (file != null)
{
product.Image = product.Id + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("//Content//ProductImages//") + product.Image);
}
context.Insert(product);
context.Commit();
return RedirectToAction("Index");
}
}
As you can see, I am storing my images in the folder 'ProductImages'. The ID's of these images are then stored in a database-table, so I will later be able to fetch the images by ID.
Now, the problem here is, that I would rather have my image folder stored on a seperate server, so it doesn't take up space on the server on which I have my project and db deployed.
I have read that this method will make the loading speed a lot faster, since images can be a pain to process due to their size.
How would I go about this?
Thanks in advance
Upvotes: 0
Views: 1419
Reputation: 13
Have you considered storing them in the wwwroot? It is a perfect place for your static content.
When the data is in the DB your DB has to iterate over all the records making it slow. When its in wwwroot it only takes up some storage. You just have to write the images to wwwroot (located at the root of the folder structure) and later retrieve them with something like:
foreach( Directory.GetFiles(...) ){....}
Upvotes: 0
Reputation: 1197
It's actually very simple.
Base64EncodedString
to the 2nd
server. Upvotes: 1