public ActionResult Subida(Ficheros model, HttpPostedFileBase fichero)
        // Cada control File del html envĂ­a un objeto de tipo HttpPostedFileBase.
        // HttpPostedFileBase es un string con t0do el flujo de datos del fichero.
        {
            if (model.Tipo == "interno")
            {
                var n = GestionarFicheros.GuardarFicheroDisco(fichero, Server);

                if (n != null)
                {
                    model.Datos  = n;
                    model.DatosB = new byte[] { 1 };
                    db.Ficheros.Add(model);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.Tipo == "base64")
            {
                var data = GestionarFicheros.ToBinario(fichero);
                if (data != null)
                {
                    model.Datos  = Convert.ToBase64String(data);
                    model.DatosB = new byte[] { 1 };
                    model.Nombre = fichero.FileName;
                    db.Ficheros.Add(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.Tipo == "binario")
            {
                var datab = GestionarFicheros.ToBinario(fichero);
                if (datab != null)
                {
                    model.Datos  = "";
                    model.DatosB = datab;
                    model.Nombre = fichero.FileName;
                    db.Ficheros.Add(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.Tipo == "azure")
            {
                var cuenta     = ConfigurationManager.AppSettings["CuentaAS"];
                var clave      = ConfigurationManager.AppSettings["ClaveAS"];
                var contenedor = ConfigurationManager.AppSettings["ContenedorAS"];

                var az = new AzureStorageUtils(cuenta, clave, contenedor);

                var n   = Guid.NewGuid();
                var ext = fichero.FileName.Substring(fichero.FileName.LastIndexOf("."));

                az.SubirFichero(fichero.InputStream, n + ext);

                model.Datos  = n + ext;
                model.Nombre = fichero.FileName;

                db.Ficheros.Add(model);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Example #2
0
        public ActionResult Subida(Ficheros model,
                                   HttpPostedFileBase fichero)
        {
            if (model.tipo == "interno")
            {
                var n = GestionarFicheros.GuardarFicheroDisco(fichero, Server);
                if (n != null)
                {
                    model.datos  = n;
                    model.datosb = new byte[] { 1 };
                    db.Ficheros.Add(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.tipo == "base64")
            {
                var data = GestionarFicheros.ToBinario(fichero);
                if (data != null)
                {
                    model.datos  = Convert.ToBase64String(data);
                    model.datosb = new byte[] { 1 };
                    model.nombre = fichero.FileName;
                    db.Ficheros.Add(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.tipo == "binario")
            {
                var datab = GestionarFicheros.ToBinario(fichero);
                if (datab != null)
                {
                    model.datos  = "";
                    model.datosb = datab;
                    model.nombre = fichero.FileName;
                    db.Ficheros.Add(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            else if (model.tipo == "azure")
            {
                var cu = ConfigurationManager.AppSettings["cuentaAzureStorage"];
                var cl = ConfigurationManager.AppSettings["claveAzureStorage"];
                var co = ConfigurationManager.AppSettings["contenedorAzureStorage"];

                var az = new AzureStorageUtils(cu, cl, co);

                var n   = Guid.NewGuid();
                var ext = fichero.FileName.Substring(fichero.FileName.LastIndexOf("."));

                az.SubirFichero(fichero.InputStream, n + ext);

                model.datos  = n + ext;
                model.nombre = fichero.FileName;

                db.Ficheros.Add(model);
                db.SaveChanges();
            }


            return(RedirectToAction("Index"));
        }