5

How do I view images from a folder using Razor MVC?

 2 years ago
source link: https://www.codesd.com/item/how-do-i-view-images-from-a-folder-using-razor-mvc.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I view images from a folder using Razor MVC?

advertisements

I am using Razor MVC and I would like to show images from "~/Content/uploads" folder. I came up with the following solution:

@foreach (FileInfo fileInfo in (new DirectoryInfo(Server.MapPath("~/Content/uploads"))
                                    .GetFiles().Where(x => x.Extension == ".jpg"))) {
    <img src="/@fileInfo
               .FullName
               .Substring(Server.MapPath("~/").Length)
               .Replace("\\", "/")"
         width="100">
}

The problem with the code is that I am taking the full file path and I am removing the Server.MapPath() prefix.

How can I simplify this code?


You can use the UrlHelper class that is available on Razor pages.

@foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/Content/uploads"), "*.jpg"))
{
    var img = new FileInfo(imgPath);
    <img src="@Url.Content(String.Format("~/Content/uploads/{0}", img.Name))" />
}

Related Articles

how to upload multiple images (from a folder) using c # asp.net?

Instead of uploading image one by one, how do i achieve to upload all the images from the folder?Use flash or java. I like the java one I posted in the link because it is extremely easy. Though the flash one is easier to customize.

view images from a folder in a specific order

In my php project I have a folder named Gallery1 (path is images/gallery1). Which contains images named 1.jpg, 2.jpg, 20.jpg, 8.jpg etc. I want to display all the images from that folder in ascending order (1.jpg, 2.jpg, 8. jpg, 20.jpg etc). Does any

How to search for images from the folder?

I have a folder named as images. I just want to fetch those images from the folder and show them randomly on web page. When the user refreshes the page, the image should change. Thank you.An idea could be: you fetch all the images names frome the ima

Openstack python API: how to download the image from the look using the api python

I am trying to write a python program to download images from glance service. However, I could not find a way to download images from the cloud using the API. In the documentation which can be found here: http://docs.openstack.org/user-guide/content/

How to call an image from a database using Codeigniter?

I hope someone can help. I am doing a college assignment at the moment, where I am building a website for purchasing cups/t shirts with a meme of their choice to be put on the cup/t-shirt. Please excuse my lack of code here, as I am just starting it

Loading images from a folder (Using C, OpenCV, Visual Studio 2008)

I am trying to load images from a folder and I am using C, OpenCV, VC++ 2008 Express Edition. My images are located on the desktop and have different names such as Tom.jpg, Flash.jpeg etc. I want to load images from this folder in my program and do s

How do I view images from a specific folder on the Android gallery

How can I display all images from a specific folder on android gallery like, for example, whatapp does. I`m using MediaScannerConnectionClient File folder = new File("/sdcard/myfolder/"); allFiles = folder.list(); SCAN_PATH=Environment.getExtern

Viewing images from a folder in PHP

I am currently creating an e-commerce website where I retrieve the vehicle details from the database and search for its image in a folder. I used the code below for searching the folder for the image, but it only show the name of the JPG file and not

How do you randomize images from a folder in VB.NET?

How do you randomize pictures being drawn from a folder to be loaded in a picturebox in VB.NET? I'm drawing a single picture at regular intervals from a folder to a picturebox. I would like to know how to randomize the selection of the picture from t

How to dissociate the image from the folder?

I am currently working on shopping cart.let me quick expain whats happening in product_info.php page. in product info page products information in displayed such as name,price,image etc.. i have made small functionality in this page.i have put 10 sma

How to optimize multiple images from the folder at once (decrease the file size) manually in webapi?

Hi all i am working on webapi i need to optimize the images from folder here i Had Uploaded images into one folder before uploading in to the destination folder i need to decrease the image size in KB of total images here i am optimizing a single ima

How do I view images from the parse.com database using javascript?

I'm looking for an answer on how to display an image in HTML from my Parse database using javascript. Reading through the docs on parse.com proved to be little help. The class where I have the image stored is called 'content'. The images are stored a

How to resize the image in the folder using php?

How to resize image in folder using php ? i try to use this code to resize image $imgPath ="test_images/english-728b1414843000.png"; to 200x200 px, but not work, how can i do that ? <?php function resize($width, $height, $imgPath, $nm){ /* Ge

Remove an image from the folder using filename in vb.net

I have backup folder where I store the images while loading them. So I need to delete the image from the backup folder when deleted from the checked listbox. So how do I do that? If CheckedListBox1.Items.Count = 0 Then MsgBox("Please load the images&

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK