0

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);
    }
5
  • 2
    And your question is? Commented Apr 15, 2015 at 15:39
  • I'm looking for a better solution Commented Apr 15, 2015 at 15:41
  • @CalebB is asking you to be more detailed in the description of your problem (source code would help) and to be more specific in what problem you are encountering. Commented Apr 15, 2015 at 15:42
  • ah ok, sorry, i have added the source code for uploading one picture at time , actually user can't upload images for a new product, he can upload images from the "edit product" view Commented Apr 15, 2015 at 15:47
  • 1
    Please explain "i want to upload images for product before save it". If the file upload fields are in the same form, they'll be posted along with the other information for the product. By the time you could save the image, you could already save the product. What is it that you're actually trying to achieve? Commented Apr 15, 2015 at 19:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.