0

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

maccettura
  • 10,514
  • 3
  • 28
  • 35
Brad Bit
  • 39
  • 1
  • 8
  • How would this make images load faster? What is the specific bottleneck being addressed? As for how you'd save files somewhere else, you'd simply replace the code which saves the files on your file system with code which saves them wherever you want to save them. Where do you want to save them? – David Sep 17 '18 at 17:34
  • Hi David thank you for your reply. I would like to save the image files in a folder which is deployed on a separate server. I have read on different blogs that this should make loading the images faster, since it puts less stress on the server which already handles everything else. This might be incorrect, but my intention is to make my page as fast as possible. – Brad Bit Sep 17 '18 at 17:45
  • And how do you access that separate server? If it can be accessed by a network file path then you'd just change the file path from what you're currently using to what you want to use. Have you tried? – David Sep 17 '18 at 17:46
  • This really depends on the shape of the other server (i.e., what does it look like to the website? Does it reveal itself via a mapped path, or does it have an endpoint you can write files to? There are countless ways how this could be implemented, for speculative benefit. If you want to ensure fast image download, store them in a CDN. Whichever you choose will have an API for you to use. –  Sep 17 '18 at 17:50
  • As far as I have understood, Server.MapPath can only access the folder-path as long as the folder is on the same server. However, from your reply this seems to be wrong. How would I make a access a path to a folder, that is not located on the same server? How do I get the pathname? – Brad Bit Sep 17 '18 at 17:51
  • @BradBit: `file.SaveAs()` can accept any path you like. Give it the fully qualified file name, including the full path, where you want to save the file. – David Sep 17 '18 at 17:56
  • Thanks for the reply, Will I gladly admit that I am a complete beginner when it comes to handling this many images on a web-application. A CDN sounds like a nice solution. Is there any particular service that you would recommend? (Amazon, digital ocean etc...) As you say, there are countless different ways to implement endpoints, so I would be happy if you could recommend one that would be good for making a page that includes a blog and an image gallery. – Brad Bit Sep 17 '18 at 18:01
  • @David The problem is, that I don't know how to access the folder on the external server - I can't get the filepath. If I could, this would be easy ofc. Will suggested creating an endpoint, or a mapped path though. Do you think this would work with what you suggested? – Brad Bit Sep 17 '18 at 18:05
  • @BradBit: *How you access* your file storage location is really something for you to find out. Is it a network file share? An API? An FTP server? Something else? You need to determine where you're going to save files before you can save them there. *If* it's a network share accessible from your web server, you need to find out what the path is. If you don't know the path, ask whoever maintains the servers what it is. – David Sep 17 '18 at 18:08

2 Answers2

1

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
Muhammad Arslan Jamshaid
  • 1,077
  • 8
  • 27
  • 48
  • It actually makes sense, thank you Muhammed. However, i am still unsure how i would go about converting them to bitmap on my server and return the path. Is it possible i can get you to write a code-snippet, or pseudo code? – Brad Bit Sep 17 '18 at 19:01
  • 1
    Convert base64string into byte array then read it in a memory stream. for reference see: https://stackoverflow.com/questions/36973960/convert-base64-string-to-bitmap-or-image-xamarin-or-android – Muhammad Arslan Jamshaid Sep 17 '18 at 19:14
0

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(...) ){....}
  • Is there a specific reason you want the images stored there? And how would i go about doing that? – Brad Bit Sep 17 '18 at 19:03
  • 1
    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(...) ){....} – Ivo van Leeuwen Sep 17 '18 at 19:32
  • 1
    The OP is already storing the files in a sub-folder of the website root. How would storing them in the website root folder greatly improve that? Why do you think a database is slower than a file system? How does this answer the OP's question about storing files *outside* of the web server? – David Sep 17 '18 at 19:42