예제 #1
0
        public object post_adjuntarArchivo(string filtros)
        {
            Resultado res              = new Resultado();
            string    nombreFile       = "";
            string    nombreFileServer = "";
            string    path             = "";
            string    url              = ConfigurationManager.AppSettings["imagen"];

            try
            {
                var    file      = HttpContext.Current.Request.Files["file"];
                string extension = System.IO.Path.GetExtension(file.FileName);

                string[] parametros = filtros.Split('|');
                int      idOT       = Convert.ToInt32(parametros[0].ToString());
                int      idUsuario  = Convert.ToInt32(parametros[1].ToString());

                nombreFile = file.FileName;

                //-----generando clave unica---
                var guid  = Guid.NewGuid();
                var guidB = guid.ToString("B");
                nombreFileServer = idUsuario + "_AdjuntarArchivo" + Guid.Parse(guidB) + extension;

                //---almacenando la imagen--
                path = System.Web.Hosting.HostingEnvironment.MapPath("~/Imagen/" + nombreFileServer);
                file.SaveAs(path);

                //------suspendemos el hilo, y esperamos ..
                System.Threading.Thread.Sleep(1000);

                if (File.Exists(path))
                {
                    ///----validando que en servidor solo halla una sola foto---
                    tbl_OrdenTrabajo_Cab objectOT;
                    objectOT = db.tbl_OrdenTrabajo_Cab.Where(p => p.id_OT == idOT).FirstOrDefault <tbl_OrdenTrabajo_Cab>();

                    OrdenTrabajo_BL obj_negocio = new OrdenTrabajo_BL();
                    obj_negocio.Set_Actualizar_archivoOT(idOT, nombreFile, nombreFileServer);


                    object respuesta = new
                    {
                        url        = url + nombreFileServer,
                        nombreFile = nombreFile
                    };

                    res.ok   = true;
                    res.data = respuesta;

                    //---si previamente habia una foto, al reemplazarla borramos la anterior
                    if (objectOT != null)
                    {
                        string nombreFileServidor = (string.IsNullOrEmpty(objectOT.nombreArchivoServidor)) ? "" : objectOT.nombreArchivoServidor;
                        path = System.Web.Hosting.HostingEnvironment.MapPath("~/Imagen/" + nombreFileServidor);

                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
                else
                {
                    res.ok   = false;
                    res.data = "No se pudo guardar el archivo en el servidor..";
                }
            }
            catch (Exception ex)
            {
                res.ok   = false;
                res.data = ex.Message;
            }

            return(res);
        }