locked
WebImage helper with multiple <input type="file"> on single form RRS feed

  • Question

  • User379720387 posted

    <form method="post" enctype="multipart/form-data">
    <div class="form-group">  
    (1) Profile Picture (camera or file) <input type="file" id="" name="Image1" accept=".jpg, .jpeg, .png"> </div> <div class="form-group"> HD Picture (camera or file) <input type="file" id="" name="Images" accept=".jpg, .jpeg, .png" multiple> </div> </form>

    I am getting the profile picture like this: 

    photo = WebImage.GetImageFromRequest();

    But how do I get the pictures from the second <input>?

    Sunday, May 27, 2018 4:12 PM

Answers

  • User36583972 posted


    Hi wavemaster,

    I am getting the profile picture like this: 

    photo = WebImage.GetImageFromRequest();

    But how do I get the pictures from the second <input>?

    You can try the following code.

    if (Request.Files.Count > 0)
        {
            for (var i = 0; i < Request.Files.Count; i++)
            {
                WebImage image = WebImage.GetImageFromRequest("Image" + i);
                if (image != null)
                {
                    var imageName = Path.GetFileName(image.FileName);
                }
            }
        }
    
    
         <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                    (1) Profile Picture (camera or file)
                    <input type="file" id="" name="Image0" accept=".jpg, .jpeg, .png">
                </div>
    
                <div class="form-group">
                    HD Picture (camera or file)
                    <input type="file" id="" name="Image1" accept=".jpg, .jpeg, .png" multiple>
                </div>
            </form>

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 3:00 AM
  • User36583972 posted

    Hi wavemaster,

    Files gives me the total number of files, but I need handle image0 different from image1

    How can I tell which one is which?

    You can use the files.AllKeys to detect which image file belongs to it's file upload control.

    You can refer the following code.

      if (IsPost)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            if (files.Count > 0)
            {
                for (int i = 0; i < files.AllKeys.Count(); i++)
                {
                    if (files.AllKeys[i] == "Image0")
                    {
                        if (files[i].FileName.Length > 0)
                        {
                            System.Web.HttpPostedFile postedfile = files[i];
                            newFileName = "Image0" + "_" + Path.GetFileName(postedfile.FileName);
                            imagePath = "~\\img\\" + newFileName;
                            var path = Server.MapPath(imagePath);
    
                            postedfile.SaveAs(path);
                        }
                    }
    
                    if (files.AllKeys[i] == "Image1")
                    {
                        if (files[i].FileName.Length > 0)
                        {
                            System.Web.HttpPostedFile postedfile = files[i];
                            newFileName = "Image1" + "_" + Path.GetFileName(postedfile.FileName);
                            imagePath = "~\\img\\" + newFileName;
                            var path = Server.MapPath(imagePath);
                            postedfile.SaveAs(path);
                        }
                    }
                }
            }
        }

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2018 8:12 AM

All replies

  • User36583972 posted


    Hi wavemaster,

    I am getting the profile picture like this: 

    photo = WebImage.GetImageFromRequest();

    But how do I get the pictures from the second <input>?

    You can try the following code.

    if (Request.Files.Count > 0)
        {
            for (var i = 0; i < Request.Files.Count; i++)
            {
                WebImage image = WebImage.GetImageFromRequest("Image" + i);
                if (image != null)
                {
                    var imageName = Path.GetFileName(image.FileName);
                }
            }
        }
    
    
         <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                    (1) Profile Picture (camera or file)
                    <input type="file" id="" name="Image0" accept=".jpg, .jpeg, .png">
                </div>
    
                <div class="form-group">
                    HD Picture (camera or file)
                    <input type="file" id="" name="Image1" accept=".jpg, .jpeg, .png" multiple>
                </div>
            </form>

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 3:00 AM
  • User379720387 posted

    Files gives me the total number of files, but I need handle image0 different from image1

    How can I tell which one is which?

    Friday, June 1, 2018 2:00 AM
  • User36583972 posted

    Hi wavemaster,

    Files gives me the total number of files, but I need handle image0 different from image1

    How can I tell which one is which?

    You can use the files.AllKeys to detect which image file belongs to it's file upload control.

    You can refer the following code.

      if (IsPost)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            if (files.Count > 0)
            {
                for (int i = 0; i < files.AllKeys.Count(); i++)
                {
                    if (files.AllKeys[i] == "Image0")
                    {
                        if (files[i].FileName.Length > 0)
                        {
                            System.Web.HttpPostedFile postedfile = files[i];
                            newFileName = "Image0" + "_" + Path.GetFileName(postedfile.FileName);
                            imagePath = "~\\img\\" + newFileName;
                            var path = Server.MapPath(imagePath);
    
                            postedfile.SaveAs(path);
                        }
                    }
    
                    if (files.AllKeys[i] == "Image1")
                    {
                        if (files[i].FileName.Length > 0)
                        {
                            System.Web.HttpPostedFile postedfile = files[i];
                            newFileName = "Image1" + "_" + Path.GetFileName(postedfile.FileName);
                            imagePath = "~\\img\\" + newFileName;
                            var path = Server.MapPath(imagePath);
                            postedfile.SaveAs(path);
                        }
                    }
                }
            }
        }

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2018 8:12 AM
  • User379720387 posted

    Thnx!

    Saturday, June 2, 2018 3:19 AM