unable to upload multiple db images with asp.net mvc

前端 未结 2 1429
小蘑菇
小蘑菇 2021-01-15 12:01

I am trying to upload several db images onto the SQL Server 2008R2. I am using ASP.NET MVC 3 in C#. What is happening is that I getting the images displayed but the problem

2条回答
  •  攒了一身酷
    2021-01-15 12:41

    The code that is listed is looping through the files, and for each one, setting both Image1 and Image2 to be the same thing. When you upload 2 files, they are both showing up as image 2 because that was the last image applied to both fields.

    Try replacing the loop with something more like this, which sets the fields one at a time if there are enough images.

    FileHandler fh = new FileHandler();
    
    if (Request.Files.Count > 0)
    {
        Createsubcat4.Image1 = fh.uploadedFileToByteArray(Request.Files[0]);
    }
    
    if (Request.Files.Count > 1)
    {
        Createsubcat4.Image2 = fh.uploadedFileToByteArray(Request.Files[1]);
    }
    
    db.AddToSubProductCategory4(Createsubcat4);
    

    If you need to open this up to allow more images in the future, you'll want to replace the Image1 and Image2 fields with a collection of images, and use your loop again to add each image in the uploaded files collection. Something like this:

    FileHandler fh = new FileHandler();
    
    foreach (HttpPostedFileBase uploadedImage in Request.Files)
    {
        Createsubcat4.Images.Add(fh.uploadedFileToByteArray(uploadedImage));
    }
    
    db.AddToSubProductCategory4(Createsubcat4);
    db.SaveChanges();
    

    EDIT:

    Now that you are saving the images correctly, you need to take a second look at your GetImage action. You'll notice that you correctly load both files into memory, however when you specify your action result (return new FileStreamResult(stream,"image/jpg");) you are only ever returning the first stream. You need a way to return the second stream when requested. There are a couple ways to go about this, add another input parameter to specify which image to load or create a second action that only returns the second one.

    To create the two action set up, your code would look something like this:

    public ActionResult GetImage1(int id)
    {
        const string alternativePicturePath = @"/Content/question_mark.jpg";
        MemoryStream stream;
    
        SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();
    
        if (z != null && z.Image1 != null)
        {
            stream = new MemoryStream(z.Image1);
        }
        else
        {
            var path = Server.MapPath(alternativePicturePath);
    
            stream = new MemoryStream();
            var imagex = new System.Drawing.Bitmap(path);
            imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);
        }
    
        return new FileStreamResult(stream,"image/jpg");
    }
    
    public ActionResult GetImage2(int id)
    {
        const string alternativePicturePath = @"/Content/question_mark.jpg";
        MemoryStream stream;
    
        SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();
    
        if (z != null && z.Image2 != null) // the difference is here
        {
            stream = new MemoryStream(z.Image2); // the difference is also here
        }
        else
        {
            var path = Server.MapPath(alternativePicturePath);
    
            stream = new MemoryStream();
            var imagex = new System.Drawing.Bitmap(path);
            imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);
        }
    
        return new FileStreamResult(stream,"image/jpg");
    }
    

    These functions are almost identical and can easily be made 1 which takes a parameter to select which image to load.

    public ActionResult GetImage(int id, int? imageNum)
    {
        imageNum = imageNum ?? 0;
    
        const string alternativePicturePath = @"/Content/question_mark.jpg";
        MemoryStream stream;
    
        SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();
    
        byte[] imageData = null;
    
        if (z != null)
        {
            imageData = imageNum == 1 ? z.Image1 : imageNum == 2 ? z.Image2 : null;
        }
    
        if (imageData != null)
        {
            stream = new MemoryStream(imageData);
        }
        else
        {
            var path = Server.MapPath(alternativePicturePath);
    
            stream = new MemoryStream();
            var imagex = new System.Drawing.Bitmap(path);
            imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);
        }
    
        return new FileStreamResult(stream,"image/jpg");
    }
    

    This function would specify the imageNum as a query parameter like:

    http://www.mydomain.com/controllerName/GetImage/{id}?imageNum={imageNum}

提交回复
热议问题