-5

I have built a simple ASP.NET MVC5 project that acts as a classifieds application for my college's students. I got everything figured out and implemented except for the images. I am struggling to find the best approach to store the images.

I came up with the following structure:

  • Ad represents the advertisement model
  • adImage represents an image(s) for an ad. AdImage has a foreign Key public int AdId { get; set; } and then public virtual Ad TheAdvertisement { get; set; }

Considerations:

  • I am using bootstrap
  • I will not consider support older IE browsers
  • I have to resize the images prior to save
  • I will be saving the images on the file system and then store their urls back into my database.

I think I know how to upload then store multiple images. I am struggling with how to implement size-limitation validation to my controller and whether it is something I should consider to implement in my project. What is the best approach when dealing with images in classified website?

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
  • 1
    It is unclear exactly what are you asking? Are you struggling with the mechanics of uploading an image(s) to an MVC controller? Or you don't know how to synchronize your uploads with a database record? Where to store the images? – Jasen Feb 12 '15 at 21:36
  • 1
    Some things you need to answer and clarify for yourself: 1) What are the images for? What is its use? Avatars, for print, thumbnail? 2) Where are you storing the uploaded images? As a blob on the database, on a file system? 3) Do you need to pre-process (resize) on the client before upload? Does your infrastructure support uploading and processing large images on the server? 4) What kind of client-side environment? Can you limit usage to only the latest browsers or do you need to support older (IE) browsers? – Jasen Feb 12 '15 at 21:38
  • Can you add the related model classes to the question? Also, what is your question? Right now, it's really unclear. What specific things are you struggling with.. just what sizes? Also, it's hard to understand your approach so we can't really clarify as to whether it's _feasible_ – Carrie Kendall Feb 13 '15 at 22:24
  • Do you have a question? You realize that a question ends in a question mark, don't you? There is not one question mark in your "question", nor is there any way for someone to understand what you are referring to without a mind meld. – NightOwl888 Feb 15 '15 at 17:23

1 Answers1

2

To Limit the size of image files, take a look at file size upload limitation in ASP.NET MVC that employs web.config file to meet your purpose.

Another way to Limit file size is writing code something like this:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    //you can put your existing save code here
    if (file != null && file.ContentLength > 10000) 
    {
        //do whatever you want with the file
    }
}

And the page File Upload ASP.NET MVC 3.0 contains good samples for you if you don't know how to write a controller to save files on file system.

Community
  • 1
  • 1
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70