Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api 2 RESTFUL Image Upload

I'm new in Web Api and I'm working on my first project. I'm working on mobile CRM system for our company.

I want to store companies logo, customers face foto etc.

I found some tutorials on this topic, but unfortunately some of them was old (doesn't use async) and the others doesn't work.

At the end I found this one: http://www.intstrings.com/ramivemula/articles/file-upload-using-multipartformdatastreamprovider-in-asp-net-webapi/ It works correctly, but I don't understand a few things.

1) Should I use App_Data (or any other folder like /Uploads) for storing this images, or rather store images in database?

2) Can I set only supported images like .jpg, .png and reject any other files?

3) How can I processed image in upload method? Like resize, reduce size of the file, quality etc?

Thank you

like image 358
Stepan Sanda Avatar asked Jan 22 '14 22:01

Stepan Sanda


1 Answers

1) We are storing files in a different location than app_data. We have a few customer groups and we gave them all a unique folder that we get from the database. Storing in database is also an option but if you go down this road, make sure that the files you are saving don't belong directly to a table that you need to retrieve often. There is no right or wrong, but have a read at this question and answer for some pros and cons.

2) If you foollowed that guide, you can put a check inside the loop to check the file ending

List<string> denyList = new List<string>();
denyList.Add(".jpg");

foreach (MultipartFileData file in provider.FileData) 
{
    string fileName = Path.GetFileName(file.LocalFileName);
    if(denyList.Contains(Path.GetExtension(fileName))
         throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

    files.Add(Path.GetFileName(file.LocalFileName)); 
}

3) Resizing images is something that I have never personally done my self, but I think you should have a look at the System.Drawing.Graphics namespace. Found a link with an accepted answer for downresize of picture: ASP.Net MVC Image Upload Resizing by downscaling or padding

like image 92
Binke Avatar answered Sep 28 '22 03:09

Binke