Brad Bit
Brad Bit

Reputation: 39

Saving images on an external server

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

Answers (2)

Ivo van Leeuwen
Ivo van Leeuwen

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

Muhammad Arslan Jamshaid
Muhammad Arslan Jamshaid

Reputation: 1197

It's actually very simple.

  • Create a WebAPI (REST API) on the 2nd server you want images to be saved on.
  • Send the images in Base64EncodedString to the 2nd server.
  • Convert them back to bitmap
  • Save on the 2nd server and return the path to be saved in 1st server's db field

Upvotes: 1

Related Questions