The correct approach would be to create, on the administrative site, a suitable interface to serve the images.
To do this, create a Controller called ImagensController
. Inside it implement the following Action:
public FileResult Imagem(int id)
{
if (id != null)
{
var imagem = db.Imagens.FirstOrDefault(x => x.ImagemId == id);
if (imagem != null)
{
var arquivoDeImagem = Server.MapPath("~/Content/images/galeria/" + imagem.imagem);
return File(arquivoDeImagem, "image/jpeg");
}
}
return null;
}
Use:
<img src="http://siteadministrativo/Imagens/Imagem/5" alt="@item.nome" />
Note that this method uses an image registration inside its administrative module, because we selected the image registration in a data context (db
).
Still, if you want to make the configuration very dynamic, you can use a configurable URL in your Web.config
:
<configuration>
...
<appSettings>
...
<add key="BaseURL" value="http://localhost:15829/" />
...
</appSettings>
...
</configuration>
To access the value on View, you can implement a Helper:
public static class ConfigurationHelper
{
public static String BaseUrl()
{
var baseUrl = ConfigurationManager.AppSettings["BaseURL"];
return baseUrl;
}
}
There it stands:
<img src="@(ConfigurationHelper.BaseUrl() + "/Imagens/Imagem/5")" alt="@item.nome" />
Finally, you can use transformation files and configure the URL exchange at the time of publication:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="BaseURL" value="http://meusite.com.br" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
You need to access the physical address of the folder where you are saving the documents... If your application (website) has permission to access the physical folder that you store the documents just go on with life...
– Caique C.
In case Full physical address as below? C: Users VICTOR Solution Admin Documents[document name]?
– Victor Morgan Fragoso
If this is the folder where you keep the documents, yes.
– Caique C.
Hello, I tried the physical path as suggested, the folder is allowed. However the image cannot be found yet. I’ve tried with Bars and Counter-Bars tbm, but none of them successfully. Have an example LINK to make sure I’m not missing? Thanks
– Victor Morgan Fragoso
Look, there’s no magic: they’re files in a folder, that’s all. For example, when you add a file in the c: images folder, there will be the images and you can access them from anywhere as long as you have access/permission. First of all, can you go to the folder and make sure the images are really there? The second step, confirmed the first, would be to access all the files from the folder via code, to see if the user who is running the application actually has permission to view the files. If yes, be happy, otherwise I believe it’s permission or the code...
– Caique C.
that
cliente.imagem
comes with the file extension?– Rafael Cabral