简体   繁体   English

在Angular中,不知道扩展名时如何加载图像?

[英]In Angular, how do I load an image when I don't know the extension?

I have a login page that I want to be able to have custom branding on. 我有一个登录页面,希望能够启用自定义品牌。 When the customer is logged in, they will be able to upload either a .gif, .jpeg, or .png image which will then be used on the login page after they have logged out. 客户登录后,他们将能够上载.gif,.jpeg或.png图片,然后在他们注销后将其用于登录页面。 The file will be stored with a generic filename in a predetermined location, so I will know all of that, I just won't know which file format was uploaded. 该文件将以通用文件名存储在预定位置,所以我将知道所有这些,而我只是不知道上载了哪种文件格式。

So basically, when a customer navigates to www.example.com/login/:id the application will load an image file from a publicly available mounted drive where the image is stored. 因此,基本上,当客户导航到www.example.com/login/:id该应用程序将从存储图像的公共安装的驱动器中加载图像文件。 The directory structure of the mounted drive where the image is stored will be something like /mnt/resource/client/:id/logo.* . 安装映像的已安装驱动器的目录结构将类似于/mnt/resource/client/:id/logo.* The customer won't be authenticated yet, so I won't be able to access any database's. 客户尚未通过身份验证,因此我将无法访问任何数据库。

My plan was to store the directory /mnt/resource/client in an environment variable, and when the login component loads up, I will append the id from the route params, and the filename, but at this point I won't know what the extension is. 我的计划是将目录/mnt/resource/client在一个环境变量中,并且在登录组件加载完毕后,我将附加来自路由参数的ID和文件名,但是到那时我还不知道扩展名是。

Is there a way to be able to load an image in the html img tag with a location something like /mnt/resource/client/0001/logo.* ? 有没有办法能够将html img标签中的图像加载到/mnt/resource/client/0001/logo.*类的位置? Also, logo.[png, jpg, gif] will be the only thing stored in this location. 另外, logo.[png, jpg, gif]将是唯一存储在此位置的东西。

EDIT: I attempted to use /mnt/resource/client/0001/logo.* as well as /mnt/resource/client/0001/logo but both give a 404. I figured it would, but thought I would try anyway. 编辑:我试图使用/mnt/resource/client/0001/logo.*以及/mnt/resource/client/0001/logo但都给出了404。我认为可以,但是我想无论如何都会尝试。

Or, is there a better way to approach/solve this problem? 或者,是否有更好的方法来解决此问题?

Also, I'm sorry if this is duplicate question, I attempted to search for related questions, but didn't find any, but also wasn't sure if I was correctly formulating my search query. 另外,如果这是重复的问题,我很抱歉,我试图搜索相关的问题,但没有找到任何问题,但是也不确定我是否正确地制定了搜索查询。

Also, let me know if there are any details I may have left out. 另外,让我知道是否有任何遗漏的细节。 Thanks in advance! 提前致谢!

In order to load an image in html, you have to know the full path. 为了以html加载图像,您必须知道完整路径。

You've got a couple of options here: 您在这里有几个选择:

  1. Store the full image name/extension in a db. 将完整的映像名称/扩展名存储在数据库中。 Your application logic would then need to only allow access to parts of the db that are "public" from your login page. 然后,您的应用程序逻辑将只需要允许从登录页面访问数据库的“公共”部分。 Your login page would request a standard image path, like example.com/resources/loginImage_clientID , and your server would do the db lookup based on that id, and return the correct image. 您的登录页面将请求标准图像路径,例如example.com/resources/loginImage_clientID ,并且您的服务器将基于该ID进行数据库查找,并返回正确的图像。
  2. Do server-side conversion of the login images so that you always use the same extension. 对登录图像进行服务器端转换,以便您始终使用相同的扩展名。

I'm also assuming you're not manually uploading these images for each client, so presumably you've already got the full image name and extension in a db somewhere so that you can verify which client is allowed to update which image. 我还假设您没有为每个客户端手动上传这些图像,因此大概您已经在某个地方的数据库中获得了完整的图像名称和扩展名,以便可以验证允许哪个客户端更新哪个图像。

I would suggest using race from RXjs : 我会建议使用raceRXjs

const extensions = ['png', 'jpg'];

race(
   ...extensions.map(ext => this.http.get(`${file}.${ext}`).pipe(
     catchError(e => NEVER),
   )),
)

The race combinator will cancel other observables in the list thus cancelling the HTTP calls. race组合器将取消列表中的其他可观察对象,从而取消HTTP调用。 catchError will turn an error into never completing observable, thus eliminating itself from the race. catchError会将错误变成无法完成的观察,从而将自己从竞争中消除。

But as stated by others, this will lead to poor performance, and you rather should learn server how to serve files without extension. 但是,正如其他人所述,这将导致性能下降,您应该学习服务器如何在不扩展的情况下提供文件。

The file will be stored with a generic filename in a predetermined location, so I will know all of that, I just won't know which file format was uploaded. 该文件将以通用文件名存储在预定位置,所以我将知道所有这些,而我只是不知道上载了哪种文件格式。

It does not matter. 不要紧。

The server should store the image at a given URL without a file extension. 服务器应将图像存储在没有文件扩展名的给定URL中。

http://www.example.com/user/:id/logo

When the browser requests the resource the server includes the content type in the HTTP header response, and the browser will display the image automatically. 当浏览器请求资源时服务器在HTTP标头响应中包括内容类型,浏览器将自动显示图像。 It does not matter if it is PNG, JPEG or GIF. 不管是PNG,JPEG还是GIF。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

If the server does not support content type detection, then it should send the image with a content-type: image/* type. 如果服务器不支持内容类型检测,则它应发送content-type: image/*

Most modern web servers allow you to configure what the content type is for a specific URL or range of URL, and most will auto detect the content type for static files. 大多数现代的Web服务器都允许您配置特定URL或URL范围的内容类型,并且大多数服务器会自动检测静态文件的内容类型。

Don't make meaningless 404 requests to your server. 不要向您的服务器发出无意义的404请求。

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

相关问题 我知道我想要什么,但我不知道该怎么做! (chrome扩展名) - I know what I want, but I don't know how to do it! (chrome extension) 没有.xml扩展名时,如何在IE中加载XML文件? - How do I load XML files in IE when they don't have a .xml extension? 如何使用jQuery异步加载图像,如何知道图像加载完成的时间? - How can I load an image asyncronously with jQuery, and how do I know when the image loading is complete? 不知道键值时,如何从JSON响应中检索键? - How do I retrieve a key from a JSON response, when I don't know the key value? 我确实知道如何解决语言/音频问题,但是我不知道如何将其作为代码组合在一起 - I do know how to solve the language/audio issue, but I don't know how to put it together as code Angular 我不使用 (load) 调用函数 - Angular I don't call a function with (load) 我不知道图像类型时的Base64图像SRC - Base64 Image SRC when I don't know the image type 我如何知道图像在加载时缓存在浏览器中 - how do I know the image is cached in browser when do the loading 我正在使用角度翻译进行标签或字符串转换,我不知道如何翻译占位符文本 - I am using angular translate to label or string conversion, i don't know how to translate the placeholder text 将数据加载到我不知道列的数据表中 - Load data into datatable where I don't know the columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM