#

 

 

Ich habe eine Bild-Datei in Asp.Net Mvc mit dem vollständigen Datei-Pfad.

Wie gibt man den relativen Ausgabepfad bei einer Webdatei an, welche man anzeigen möchte.

 

Vorgabe:

Der absolute Pfad einer .jpg-Foto-Datei ist bekannt.

Beispiel: "C:\\aspNet_test\wwwroot\User_Files\Image_1.jpg"

 

Aufgabe:

Wie gibt man in asp.net mvc core den relativen Pfad für den Client-Browser an?

 

Lösung:

man gibt nur den teil ab wwwroot mit Frontslash beginnend an

Lösungsbeispiel: "/User_Files/Image_1.jpg"

 

 

Wert übergabe in der Controller-Codebehind Datei in asp.Net Mvc

//< output >

ViewData["FileUploaded"] = "/User_Files/User_Images/Original/" + sTarget_Filename;

return View();

//</ output >

 

 

Auf der Ausgabe View in asp.Net Mvc

<img src="@ViewData["FileUploaded"]" alt="Uploaded Image" />

 

 

 

 

View-Code Datei

@{

    Layout = null;

}

 

<!DOCTYPE html>

 

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Upload_Image</title>

</head>

<body>

    <br /><br />

    <span style="margin-top:10px;"> Upload Your Profil Image:</span>

    <br />

    <form asp-controller="Image" asp-action="Upload_User_Profil_Image" method="post"

          enctype="multipart/form-data">

 

        <input type="file" name="uploaded_File" />

        <br />

        <button type="submit" style="">Upload</button>

    </form>

 

    <br />

    <br />

    @if (ViewData["FileUploaded"]!=null)

    {

    <img src="@ViewData["FileUploaded"]" alt="Uploaded Image" />

    }

 

    @if (ViewData["Error"]!=null)

    {

    <label>Error= @ViewData["Error"]</label>

    }

 

    <br />

</body>

</html>

 

Controller Datei

In der Codebehind Controller-Datei

        [Authorize]

        [HttpPost] //Postback

        public async Task<IActionResult> Upload_User_Profil_Image(IFormFile uploaded_File)

        {

            //--------< Upload_ImageFile() >--------

            string sResult = "";

 

 

            //< check >

            if (uploaded_File == null || uploaded_File.Length == 0)

            {

                ViewData["Error"] = "Error:file not selected";

                return View(ViewData);

            }

 

            if (uploaded_File.ContentType.IndexOf("image", StringComparison.OrdinalIgnoreCase) < 0)

            {

                ViewData["Error"] = "Error:This file is not an image";

                return View(ViewData);

            }

            //</ check >

 

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await Common.ExtensionMethods.getIDUser_Number(this.User, _context);

            //--</ Get User ID >--

 

            //< init >

            string sImage_Folder = "User_Images";

            string sTarget_Filename = "User_Image_" + IDUser + ".jpg";

            //</ init >

 

            //< get Path >

            string sPath_WebRoot = _appEnvironment.WebRootPath;

            string sPath_of_Target_Folder = sPath_WebRoot + "\\User_Files\\" + sImage_Folder + "\\";

            string sFile_Target_Original = sPath_of_Target_Folder + "\\Original\\" + sTarget_Filename;

            //string sImage_Filename_Original = sPath_of_Target_Folder + uploaded_File.FileName;

            //</ get Path >

 

            //< Copy File to Target >

            using (var stream = new FileStream(sFile_Target_Original, FileMode.Create))

            {

                await uploaded_File.CopyToAsync(stream);

            }

            //</ Copy File to Target >

 

            //< resize >

            Image_resize(sFile_Target_Original, sPath_of_Target_Folder + "\\400\\" + sTarget_Filename, 400);

            //</ resize >

 

            //string sResult= await Safe_Uploaded_Image(ref uploaded_File, "User_Images");

 

 

            //< output >

            ViewData["FileUploaded"] = "/User_Files/User_Images/Original/" + sTarget_Filename;

            return View();

            //</ output >

            //--------</ Upload_ImageFile() >--------

        }

 

 

Mobile

.

0123movie.net