public ActionResult Archivos()
        {
            if (!this.currentUser())
            {
                return(RedirectToAction("Ingresar"));
            }

            MenuNavBarSelected(2);

            ArchivosBL archivosBL = new ArchivosBL();

            return(View(archivosBL.getArchivos()));
        }
        //
        // GET: /Archivos/
        public FileStreamResult Index(string path)
        {
            ArchivosBL archivosBL = new ArchivosBL();
            Archivo    archivo    = archivosBL.getByUri(path);

            if (archivo != null)
            {
                var    filePath    = Server.MapPath(CONSTANTES.FILES_PATH) + archivo.Uri + archivo.Extension;
                string contentType = "application/octet-stream";
                switch (archivo.Extension.ToLower())
                {
                case ".doc": contentType = "application/msword"; break;

                case ".xls": contentType = "application/vnd.ms-excel"; break;

                case ".pdf": contentType = "application/pdf"; break;

                case ".jpg":
                case ".jpeg":
                case ".jpe": contentType = "image/jpeg"; break;

                case ".png": contentType = "image/png"; break;

                case ".zip": contentType = "application/zip"; break;

                case ".rar": contentType = "application/x-rar-compressed"; break;

                default: contentType = "application/octet-stream"; break;
                }
                try
                {
                    Stream file = new FileStream(filePath, FileMode.Open);
                    Response.ContentType = contentType;
                    return(File(file, contentType, archivo.Nombre));
                }
                catch (FileNotFoundException e) {
                    Response.StatusCode = 404;
                    throw e;
                }
            }
            Response.StatusDescription = "Archivo no encontrado";
            Response.StatusCode        = 404;
            return(null);
        }
Exemple #3
0
        public HttpResponseMessage Upload()
        {
            // Get a reference to the file that our jQuery sent.  Even with multiple files, they will all be their own request and be the 0 index

            if (HttpContext.Current.Request.Files.Count > 0)
            {
                var file          = HttpContext.Current.Request.Files[0];
                var filesUploaded = new List <Archivo>();
                if (file != null && file.ContentLength > 0)
                {
                    var fileName  = Path.GetFileName(file.FileName);
                    var extension = Path.GetExtension(file.FileName);
                    var uri       = GenerateGUID.GenerateId();
                    var path      = Path.Combine(HttpContext.Current.Server.MapPath(CONSTANTES.FILES_PATH), uri + extension);
                    file.SaveAs(path);
                    ArchivosBL archivosBL = new ArchivosBL();
                    var        archivo    = new Archivo()
                    {
                        Nombre    = fileName,
                        Extension = extension,
                        Estado    = true,
                        Uri       = uri
                    };
                    string absolutePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + "/" + CONSTANTES.URI_ARCHIVO;
                    archivosBL.add(archivo, absolutePath);
                    filesUploaded.Add(archivo);
                }
                // Now we need to wire up a response so that the calling script understands what happened
                HttpContext.Current.Response.ContentType = "text/plain";
                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                var result     = System.Web.Helpers.Json.Encode(filesUploaded);

                HttpContext.Current.Response.Write(serializer.Serialize(result));
                HttpContext.Current.Response.StatusCode = 200;
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
        }