asp.netbytehttpfilecollection

Converting HttpFileCollection to byte and stream the data to Image Control in asp.net


    HttpFileCollection oHttpFileCollection = e.PostedFiles;
    HttpPostedFile oHttpPostedFile = null;
    if (e.HasFiles)
    {
        for (int n = 0; n < e.Count; n++)
        {
            oHttpPostedFile = oHttpFileCollection[n];
            if (oHttpPostedFile.ContentLength <= 0)
                continue;
            else
                oHttpPostedFile.SaveAs(Server.MapPath("Files") + "\\" + System.IO.Path.GetFileName(oHttpPostedFile.FileName));
    }

How can I convert HttpFileCollection to byte and read the stream and then display the image to Image Control in asp.net

Thanks


Solution

  • HttpFileCollection oHttpFileCollection = e.PostedFiles;
    HttpPostedFile oHttpPostedFile = null;
    if (e.HasFiles)
    {
        for (int n = 0; n < e.Count; n++)
        {
            oHttpPostedFile = oHttpFileCollection[n];
            if (oHttpPostedFile.ContentLength <= 0)
            {
                continue;
            }
            else
            {
                var filename = Path.GetFileName(oHttpPostedFile.FileName);
                var path = Path.Combine(Server.MapPath("~/Files/"), filename);
                oHttpPostedFile.SaveAs(path);
    
                // Now you could display each image in a dynamically added Image
                // control to the page:
                Image image = new Image();
                image.ImageUrl = "~/Files/" + filename;
    
                // I assume that you have a reference to the current page
                // so that you could append image controls to it.
                // You could also append the images to a placeholder or a Panel
                // on your WebForm
                this.Controls.Add(image);
            }
    }