Home > OS >  How to insert a downloaded image into HTML code?
How to insert a downloaded image into HTML code?

Time:07-17

I'm a junior dev trying to build a portfolio site and want to upload a pic of me that is already downloaded on my PC. When I type in an src of an image that exists somewhere online it clearly appears on my live server but when I insert the src of an image existing somewhere within my PC's local drives it doesn't appear. Is it that I have to upload this image somewhere online (e.g. google drive) and then type in its new src or is there a way to upload offline images?
Hope someone can help!

CodePudding user response:

Create a folder called assets in your project and add all images and any other files you may need. Then add the path to your image in src

CodePudding user response:

there are some options to do it.

  1. The more preferable is creating a folder let's say Src or Images. Then download any images into the folder using classes from a namespace System.IO (Path, Directory, ...). But be sure that your image names extension are unique. Also, you can store in a DB file name and path the related file on your server and userId to be sure who added the file.
  2. Another option is to store byte arrays in DB. Adding action for returning your file
public async Task<AcitonResult> GetImage(int id)
{
    byte[] imgBytes = await GetImageFromDbById(id);
    if (imgBytes == null)
        return NotFound();
    return File(imgBytes, "image/jpg");
}

Asking an image

<img src="/**YourControllerName**/GetImage/**@imageId**">
  • Related