Saidi omar
Saidi omar

Reputation: 81

Upload images asp.net MVC5

I have an mvc 5 application in which a product has one or many images.in the create form, i want to upload images for product before save it.

I'm trying to use a temp folder to save images and once a product is saved, we save images in database, but i don't think it's the best solution

public ActionResult ProductPictureAdd(int pictureId, int productId)
    {
        if (pictureId == 0)
            throw new ArgumentException();

        var product = _productService.GetById(productId);
        if (product == null)
            throw new ArgumentException("No product found with the specified id");


        if (_workContext.CurrentUser != null && product.UserId != _workContext.CurrentUser.ID)
            return RedirectToAction("List");

        _productService.InsertProductPicture(new ProductPicture
        {
            PictureId = pictureId,
            ProductId = productId,
            CreatedOnUtc = DateTime.UtcNow,
            UpdatedOnUtc = DateTime.UtcNow
        });



        return Json(new { Result = true }, JsonRequestBehavior.AllowGet);
    }

Upvotes: 0

Views: 167

Answers (1)

Davidson Sousa
Davidson Sousa

Reputation: 1353

There are a couple of ways to do it but one sentence caught my attention:

i want to upload images for product before save it

This means for me that you should use AJAX to upload the images. I explain how to do it in this article I wrote a while ago: http://davidsonsousa.net/en/post/how-to-upload-a-file-using-mvc-3-and-ajax

we save images in database, but i don't think it's the best solution

It depends on what exactly you want. I normally save the images into App_Data and then create a handler to get them. But I would save them into the database without any problem in case I would not be able to save them in any folder due project restrictions.

Upvotes: 1

Related Questions