简体   繁体   English

上传图片asp.net MVC5

[英]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. 我有一个mvc 5应用程序,其中产品具有一个或多个images.in创建表单,我想在保存之前上传该产品的图片。

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);
    }

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. 对我来说,这意味着您应该使用AJAX上传图像。 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 我在前一段时间写的这篇文章中解释了如何做: 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. 我通常将图像保存到App_Data ,然后创建一个处理程序来获取它们。 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. 但是我会毫无问题地将它们保存到数据库中,以防万一由于项目限制我无法将它们保存在任何文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM