简体   繁体   English

HTML 图像不显示,而 src url 工作

[英]HTML Image not displaying, while the src url works

<img class="sealImage" alt="Image of Seal" src="file:///C:/Users/Anna/Pictures/Nikon%20Transfer/SEALS%20PROJECT/j3evn.jpg">

That doesn't display an image, just the alt.那不显示图像,只显示alt。 But if I go to但如果我去

file:///C:/Users/Anna/Pictures/Nikon%20Transfer/SEALS%20PROJECT/j3evn.jpg

in a browser, the image displays.在浏览器中,图像显示。

I'm hosting this on xampp, on a windows machine right now.我现在在 Windows 机器上的 xampp 上托管它。

I've tried different browsers, and with and without %20 for space, but I know that with is the correct way.(It worked with both, actually)我已经尝试过不同的浏览器,并且有和没有 %20 的空间,但我知道 with 是正确的方法。(实际上它适用于两者)

And I know the images will only be visible on the machine that's hosting it, that's not a problem.而且我知道图像只会在托管它的机器上可见,这不是问题。

Your file needs to be located inside your www directory.您的文件需要位于 www 目录中。 For example, if you're using wamp server on Windows, j3evn.jpg should be located,例如,如果您在 Windows 上使用 wamp 服务器,则应该找到 j3evn.jpg,

C:/wamp/www/img/j3evn.jpg

and you can access it in html via你可以通过 html 访问它

<img class="sealImage" alt="Image of Seal" src="../img/j3evn.jpg">

Look for the www, public_html, or html folder belonging to your web server.查找属于您的 Web 服务器的 www、public_html 或 html 文件夹。 Place all your files and resources inside that folder.将所有文件和资源放在该文件夹中。

Hope this helps!希望这可以帮助!

My images were not getting displayed even after putting them in the correct folder, problem was they did not have the right permission, I changed the permission to read write execute.即使将它们放在正确的文件夹中,我的图像也没有显示,问题是它们没有正确的权限,我将权限更改为读写执行。 I used chmod 777 image.png.我使用了 chmod 777 image.png。 All worked then, images were getting displayed.然后一切正常,图像开始显示。 :) :)

It wont work since you use URL link with "file://".由于您使用带有“file://”的 URL 链接,因此它不会起作用。 Instead you should match your directory to your HTML file, for example:相反,您应该将您的目录与您的 HTML 文件相匹配,例如:

Lets say my file placed in:可以说我的文件放在:

C:/myuser/project/file.html

And my wanted image is in:我想要的图像在:

C:/myuser/project2/image.png

All I have to do is matching the directory this way:我所要做的就是以这种方式匹配目录:

<img src="../project2/image.png" />

You can try just putting the image in the source directory.您可以尝试将图像放在源目录中。 You'd link it by replacing the file path with src="../imagenamehere.fileextension In your case, j3evn.jpg.您可以通过用src="../imagenamehere.fileextension替换文件路径来链接它在您的情况下,j3evn.jpg。

我的问题不包括图像名称之前的 ../

background-image: url("../image.png");

My problem was that I had / in front of the path (assets is a dir), such as:我的问题是我在路径前面有/ (资产是一个目录),例如:

<img src="assets/images/logo.png>" is correct one, while <img src="assets/images/logo.png>"是正确的,而

<img src="/assets/images/logo.png>" returns an error. <img src="/assets/images/logo.png>"返回错误。

将图像文件夹的名称更改为 img ,然后使用 HTML 代码

Not a direct answer to this particular question.不是这个特定问题的直接答案。 But in case, you are giving the image address correctly like this,and wondering why you don't see the image in the output,但是,如果您像这样正确地提供图像地址,并且想知道为什么在输出中看不到图像,

<img src ="../../../public/image.jpg"/> 

Directly give the image name like ,直接给出图像名称,如

<img src="image.jpg"/>

Or if you have a folder inside public , like say icons , give it like或者,如果您在public中有一个文件夹,比如说图标,就给它

<img src="/icons/logo.jpg"/>

This is because, React already expects you to have all your images inside public folder.这是因为,React 已经期望您将所有图像都放在公共文件夹中。 So you don't need to explictly give it.所以你不需要明确地给它。

The simple solution is:简单的解决方案是:

1.keep the image file and HTML file in the same folder. 1.将图像文件和HTML文件保存在同一个文件夹中。

2.code: <img src="Desert.png"> // your image name. 2.code: <img src="Desert.png"> // 你的图片名称。

3.keep the folder in D drive. 3.将文件夹保存在D盘。

Keeping the folder on the desktop(which is c drive) you can face the issue of permission.将文件夹保留在桌面(即 C 盘)上,您可能会遇到权限问题。

In your angular.json folder structure should be like this在你的 angular.json 文件夹结构应该是这样的

"assets": [             
  "src/assets",
  "src/favicon.ico"
],

not this不是这个

"assets": [     
  "src/favicon.ico",
  "src/assets",
],

This solved my problem.这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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