Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Error when trying to display an image saved in a computer file

I am developing an application which allows me to bring some files saved from another server for that I have a code made in Asp.net which I consume with Javascript to bring the image, but when I get the image to show it, the following generates me error, Not allowed to load local resource: file: /// C: /Users/usuario/Desktop/imagenes/prfil/descarga.png, but when I copy the same link in the browser if it brings me the image:
This is my code .net:

public IHttpActionResult Get(string nombreArchivo)
{
    string directorio = "C:\Users\usuario\Desktop\imagenes\"
    string ruta = directorio + nombreArchivo;
    if (File.Exists(ruta))
    {
        var result = new
        {
            imagen = ruta.Replace('\\', '/')
        }
        return Ok(result);
    }
    else
    {
        var result = new
        {
            imagen = "No existe la imagen"
        }
        return Ok(result);
    }
}

And this my code JavaScript:

const file = async () => {
    const res = await fetch(`http://localhost:64108/api/Archivos/?nombreArchivo=perfil/descarga.png`);
    const datos = await res.json();

    foto.style.backgroundImage = `url('${datos.imagen}')`;
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Browsers use the local file:// protocol to load local files, which is only allowed for local calls. Same thing with HTTP protocol; it won't work to use this protocol followed by the full path of a local file. Yet, you have at least two options here. You either provide a public folder within your application's root directory, where you can access the file using a relative URI, which is the safer way of doing it. Another possible approach is to return a file instead of a path. For this one, you may do something like this:

Javascript

let url = "http://localhost:64108/api/Archivos/?nombreArchivo=perfil/descarga.png";
let init = {
    method: 'GET',
    headers: {
        'Accept': 'image/png'
    }
};

fetch(url, init)
    .then(function (res) {
        // ...
    })
    .catch(function (err) {
        // ...
    });

C#

public IHttpActionResult Get(string nombreArchivo)
{
    string directorio = "C:\\Users\\usuario\\Desktop\\imagenes\\";
    string ruta = directorio + nombreArchivo;

    if (System.IO.File.Exists(ruta))
        return File(path, Request.Headers["Accept"]); // We've already set this to "image/png" in the javascript part

    else return BadRequest("No existe la imagen");
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda