Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

Instructions: How to outsource user files from a Asp.Net core project separately so that the Asp.Core code project is really physically separated from the real user files.

 

User files are dynamic content created by web users. Usually it is upload of photos, Office documents, videos or code files

Asp.Net Core 3, . Net5

example:

The upload photos in the web application should be stored in a neighboring folder next to the Asp Core application.

 

 

To offload directories to Asp.Net Core 3 / . Net5 you have to extend the UseStaticFiles and the FileProvider in the startup file. Then you can address the files outside the project directory with the physical path in the controller and address the HTML outputs again like a subdirectory.

 

 

1.               Physical directories

Loading the beim init of the controller

When starting a controller, Asp.net Core 3+ loads the physical path to the application and the wwwRoot via the IWebHostEnvironment

Physical directories in:_hostingEnvironment

private readonly ApplicationDbContext _dbContext;

private readonly IWebHostEnvironment _hostingEnvironment;   //

public ArticlesController(ApplicationDbContext dbContext, IWebHostEnvironment hostingEnvironment)

{

    //----< Init: Controller >----

    _dbContext = dbContext;

    _hostingEnvironment = hostingEnvironment;

    //----</ Init: Controller >----

}

 

 

Name

Value

hostingEnvironment

{Microsoft.AspNetCore.Hosting.HostingEnvironment}

ApplicationName

"CodeDocu"

ContentRootFileProvider

{Microsoft.Extensions.FileProviders.PhysicalFileProvider}

ContentRootPath

"D:\\Programmierung\\Web\\CodeDocu\\CodeDocu"

EnvironmentName

"Development"

WebRootFileProvider

{Microsoft.Extensions.FileProviders.CompositeFileProvider}

WebRootPath

"D:\\Programmierung\\Web\\CodeDocu\\CodeDocu\\wwwroot"

 

 

 

1.               Specifying the Physical Directory for User Files

Controller Actin

The path for the upload of user files is usually only necessary in a controller action.

The real path can either be entered directly by string, or as defined here in a neighboring folder of the parent folder.

 

//< init Upload Path >

//*get path to user files images folder "D:\\Programmierung\\Web\\CodeDocu\\User_Files\\Articles\\Images\\

 

string sPath_Application = _hostingEnvironment.ContentRootPath; 

//folder "D:\\Programmierung\\Web\\CodeDocu\\CodeDocu"

 

string sPath_Userfiles = sPath_Application.Substring(0, sPath_Application.LastIndexOf("\\"));  //parent folder "D:\\Programmierung\\Web\\CodeDocu"

 

sPath_Userfiles = Path.Combine(sPath_Userfiles, "User_Files");  //User files folder "D:\\Programmierung\\Web\\CodeDocu\\User_Files\\Articles\\Images\\"

 

string path_for_Uploaded_Files = Path.Combine(sPath_Userfiles, "Articles\\Images\\");

//< init Upload Path >

 

Server access to external user  folder

Here a file is defined in the User_Folder an image file as an example.

This can then be accessed and edited regularly with System.IO.File.

The file is edited by the server, the web app, even though it is located in a parent directory.

Because the app code is really separate from the user files.

 

//< Filename >

string image_webname = "Image_" + IDArticle + "_" + last_ImageNr + "." + sExtension;

string new_FullFilename_on_Server = Path.Combine( path_for_Uploaded_Files, image_webname);

//</ Filename >

 

 

//< Copy File to Target >

if (System.IO.File.Exists(new_FullFilename_on_Server)) { System.IO.File.Delete(new_FullFilename_on_Server); }

using (FileStream stream = new FileStream(new_FullFilename_on_Server, FileMode.Create))

{

    await uploaded_file.CopyToAsync(stream);

}

//</ Copy File to Target >

 

//----< check Properties >----

Image originalImage = Image.FromFile(new_FullFilename_on_Server);

if (originalImage.PropertyIdList.Contains(0x0112))

 

 

Server editing of files

As you can see in the editing, photos and files are created in the external folder without any problems

 

 

 

Display of user files in the HTML frontend

The files of the /User_Files directory are displayed directly with the / Frontslash character

<img src="/User_Files/Articles/Images/Image_2598_7_blog.jpg" style="width: 100%; max-width: 100%; height: auto; display: inline-block;">

 

 

 

Adaptation of the startup.cs

In order for the external User_Files folder to be connected like a local path, the Asp.Net Core 3+ . Net5+ the UseStaticFiles can be customized

 

In Startup.cs ->Configure(..)

app.UseStaticFiles();   //*path to: wwwroot, css, js, bootstrap

 

//*path to user content folder

// using Microsoft.Extensions.FileProviders;

// using System.IO;

app.UseStaticFiles(new StaticFileOptions

{

    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "..\\User_Files")),

    RequestPath = "/User_Files"

});

 

 

Mobile

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de