简体   繁体   中英

how to access static files from Another server in .net Core?

I have 2 Web Apps in.Net Core, the first is called ' MiddelwareApp ' that is public To the inte.net and Receive Requests from clients in encrypted Case Then Decrypt them and sends Requests to the second App called " MainApp " located at another server.

'MainApp' contains My Controllers In addition to Static Files in WWWRoot. Sending Requests from MiddlewareApp, mapping to controllers in MainApp, and getting the correct data are done without any problem. But the problem with Static Files What I want is to access my static files in MainApp from Middlewareapp in Another server.

Currently, I am in the local host MainApp in port 7000 and middleware in port 7001

In program File of mainApp:

.........
app.UseHttpsRedirection();

//to allow access files in wwwroot without any server-side processing. 
app.UseStaticFiles(new PathString("/app-Assets"));
// to allow access files not in wwwroot
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), @"Assets")),
    RequestPath = new PathString("/app-Assets")
});
app.UseRouting();
......

I tried In the program file of Middleware app:

...
app.UseFileServer(

    new FileServerOptions
    {
        
         FileProvider = new PhysicalFileProvider(@"\\https:\\localhost:7000\app-Assets"),

// this is url of  static file in Main app
RequestPath = new PathString("/app-Assets"),
EnableDirectoryBrowsing = false
});
...

in my HTML I want to access Image directly from URL of middleware port 7001

<img src="https://localhost:7001/app-Assets/myImage.jpg"\>

I expected that image src would send request To middleware then 'UseFileServer' in its program access static file of Main app on another server

The error say

System.IO.DirectoryNotFoundException: '//https://localhost:7000/app-Assets/'

but if i make path '{LocalDrive}/app-Assets/' it work

I Follow some guidelines for that but now work for me because

https://www.jauernig-it.de/asp.net-coreiis-serving-content-from-a-file-share/

How to use static content in .NET Core from server location?

if some one need some additional details,I will do

Thanks for your helping

Edit: The solution Worked For me

Using app.UseFileServer() method in Middleware App not Work at All,So I tried another way that make the same effect

[Route("GetImage")]
[HttpGet]
public async Task<IActionResult> Get(string filePath, bool inline = true)
{
    if (string.IsNullOrWhiteSpace(filePath))
        throw new ArgumentNullException(nameof(filePath));

    filePath = Path.Combine("app-Assets\\", filePath);

    var httpClient = _httpClientFactory.CreateClient("AnotherServerDirectory");
    var response = await httpClient.GetAsync(filePath);
    var ms = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
    var ext = Path.GetExtension(filePath).ToLowerInvariant();

    var cd = new ContentDisposition
    {
        Inline = inline // false = prompt the user for downloading;  true = browser to try to show the file inline
    };

    Response.Headers.Add("Content-Disposition", cd.ToString());
    return new FileStreamResult(ms, GetMimeTypes()[ext]);
} 

I set 'Content-Disposition' to control if I want to download the image or display it directly from <img src=""> tag then in Html I make

<img src="https://localhost:700/GetImage?filePath=myImage.jpg"\>

If you only want the result, then you can use server to return to the client.

Example:

  1.  <img src="https://localhost:7001/app-Assets?image=myImage.jpg"\>
  2. Stream image from controller's

    [HttpGet] [Route("app-Assets")] public IActionResult AssetsImage(string image) { var image = System.IO.File.OpenRead($"app-Assets/{image}"); return File(image, "image/jpeg"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM