none
Multiupload enviando somente uma foto RRS feed

  • Pergunta

  • Bom dia, gostaria de ajuda com o código abaixo, pois o mesmo está enviando somente uma foto para o servidor e não está realizando a gravação no banco de dados.

    using System;
    using System.Configuration;
    using MySql.Data.MySqlClient;
    using System.Web;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
    using ImageUtils;
    
    namespace Administrador
    {
        public partial class EnviarFotos : System.Web.UI.Page
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
            MySqlCommand cmd = new MySqlCommand();
            MySqlDataReader reader;
    
            public bool ThumbnailCallback()
            {
                return false;
            }
    
            public bool ImageCallback()
            {
                return false;
            }
    
            string _Id = System.Web.HttpContext.Current.Request.QueryString["id"].ToString();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                
    
            }
    
            protected void btnEnviarFotos_Click(object sender, EventArgs e)
            {            
    
                try
                {
                    HttpFileCollection hfc = Request.Files;
    
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
    
                        int maxWidth = 640;
                        int maxHeight = 480;
    
                        Bitmap originalImage = new Bitmap(hfc[i].InputStream);
                        int newWidth = originalImage.Width;
                        int newHeight = originalImage.Height;
                        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
    
                        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                        {
                            newWidth = maxWidth;
                            newHeight = (int)Math.Round(newWidth / aspectRatio);
                        }
                        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                        {
                            newHeight = maxHeight;
                            newWidth = (int)Math.Round(newHeight * aspectRatio);
                        }
    		    //QUANDO UTILIZO SOMENTE ESTE CÓDIGO ELE ENVIA TODAS AS IMAGENS PARA O SERVIDOR E SALVA NO BANCO DE DADOS, MAS PRECISO
    //MAS PRECISO REDIMENSIONAR AS IMAGENS E DIMINUIR SEU TAMANHO EM KB. //hpf.SaveAs(Server.MapPath("\\admin\\Galeria\\Images") + "\\" + System.IO.Path.GetFileName(hpf.FileName.Replace(" ", "-").Replace("%20", "-"))); //Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "Sucesso", "alert('Fotos enviadas com sucesso!')", true); Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight); originalImage.Dispose(); ImageCodecInfo codecInfo = Images.GetEncoderInfo(ImageFormat.Jpeg); EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(Encoder.Quality, 75L); System.Drawing.Image.GetThumbnailImageAbort IdummyCallBack; IdummyCallBack = new Image.GetThumbnailImageAbort(ImageCallback); String UploadImage = Server.MapPath("\\admin\\Galeria\\Images") + "\\" + System.IO.Path.GetFileName(hpf.FileName.Replace(" ", "-").Replace("%20", "-")); String UploadThumb = Server.MapPath("\\admin\\Galeria\\Thumbnails") + "\\" + System.IO.Path.GetFileName(hpf.FileName.Replace(" ", "-").Replace("%20", "-")); Image ImageUp = newImage.GetThumbnailImage(newWidth, newHeight, IdummyCallBack, IntPtr.Zero); ImageUp.Save(UploadImage, codecInfo, parameters); ImageUp.Dispose(); System.Drawing.Image.GetThumbnailImageAbort dummyCallBack; dummyCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallback); Image Thumbnail = newImage.GetThumbnailImage(160, 120, dummyCallBack, IntPtr.Zero); Thumbnail.Save(UploadThumb, codecInfo, parameters); Thumbnail.Dispose(); String Fotos = hpf.FileName; String strQuery = "insert into galeria_fotos values (null, '" + Fotos.Replace(" ", "-").Replace("%20", "-") + ", '" + _Id + "')"; con.Open(); cmd.CommandText = strQuery; cmd.ExecuteNonQuery(); con.Close(); } } catch (Exception ex) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "Erro", "alert('" + ex.Message + "')", true); } finally { Response.Redirect("Galerias.aspx"); } } } }

    O código da dll ImageUtils:

    using System;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using BufferedImage = System.Drawing.Image;
    
    namespace ImageUtils
    {
        public static class Images
        {
            private static string ImageToBase64(BufferedImage image, string imageformat)
            {
                ImageFormat format;
                switch (imageformat)
                {
                    case "png":
                        format = ImageFormat.Png;
                        break;
                    case "bmp":
                        format = ImageFormat.Bmp;
                        break;
                    case "gif":
                        format = ImageFormat.Gif;
                        break;
                    case "ico":
                        format = ImageFormat.Icon;
                        break;
                    case "jpg":
                        format = ImageFormat.Jpeg;
                        break;
                    case "wmf":
                        format = ImageFormat.Wmf;
                        break;
                    default:
                        format = null;
                        break;
                }
    
                using (MemoryStream ms = new MemoryStream())
                {
                    // Convert Image to byte[]
                    image.Save(ms, format);
                    byte[] imageBytes = ms.ToArray();
    
                    // Convert byte[] to Base64 string
                    string base64String = Convert.ToBase64String(imageBytes);
                    return base64String;
                }
            }
    
            public static string getImageHexaBase64(BufferedImage bufferedImage, string imageformat)
            {
                return ImageToBase64(bufferedImage, imageformat);
            }
    
            public static ImageCodecInfo GetEncoderInfo(ImageFormat format)
            {
                return ImageCodecInfo.GetImageEncoders().ToList().Find(delegate(ImageCodecInfo codec)
                {
                    return codec.FormatID == format.Guid;
                });
            }
        }
    }


    quinta-feira, 29 de novembro de 2012 10:06

Todas as Respostas