I want to use an iframe
in my Blazor server app to display PDF resources. I have used UseStaticFiles
middleware to serve the resource location to iframe
, but it does not work.
Here is the code snippets, where test.pdf has been placed in "C:\temp\CRM\Static\temp\pdf\test.pdf"
:
Program.cs
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(@"c:\temp\CRM\Static"),
RequestPath = "/Static",
});
page. Razor
<iframe src="temp/pdf/test.pdf"></iframe>
Update
Actually my application is placed in c:\temp\CRM\Services directory as builder.Environment.ContentRootPath shows, and the PDF file is placed in c:\temp\CRM\Static\temp\pdf directory.
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, @"..\Static")),
RequestPath = "/Static"
});
It means that, I tried to provide a available path to my resource for iframe, but It does not work. It seems that the symbol "..\Static" is not correct.
Update 2
The ".." symbole is now corrected, but it does not work also:
string sContentRootPath = builder.Environment.ContentRootPath;
int idx = sContentRootPath.LastIndexOf("\\");
idx = sContentRootPath.LastIndexOf("\\", idx - 1);
string sContentRoot = sContentRootPath.Substring(0, idx);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new
PhysicalFileProvider(Path.Combine(sContentRoot, "Static")),
RequestPath = "/Static",
});
Can anyone tell me, what's my mistake?
Now I have found my mistake and the right answer. With the last update 2, i need to correct ifarme src attribute like this:
<iframe src="/Static/temp/pdf/test.pdf"></iframe>
and it works now.