Html背景图片URL路径是巨大的 [英] Html background image URL path is gigantic

查看:533
本文介绍了Html背景图片URL路径是巨大的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,我将在后面主持一个背景图片,在那里我可以从我的本地PC选择任何图像。我试图在点击按钮时将URL路径保存到我的数据库中,以便我可以在下次加载页面时检索图像路径,而无需再次从我的PC导入图像。

但我找到了URL路径超大。例如,我的桌面路径中有一个图像(C:\Desktop\Image1.jpg)。但是当我尝试使用javascript检索值时:

Hi, I have a div in which i will host a background image behind where i can choose any image from my local PC. I tried to save the URL path into my DB upon button click so that i can retrieve the image path next time i load the page without having to import the image again from my PC.
But i have found that the URL path is super huge. For example, i have an image in my desktop path(C:\Desktop\Image1.jpg). But when i tried to retrieve the value using javascript:

var bg = $(this).css('background-image');
bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");



,它变成了超级巨大的网址,总共有20万个字符数,我无法保存它。



我尝试了什么:



1.在线查看建议,但所有人都使用类似的方式来检索图片网址。只有不同的是在线文章获取网站URL而不是本地PC图像路径。


, it became super huge url with a total of 200k plus characters count where it is impossible for me to save it.

What I have tried:

1. Look online for suggestion but all are using similar way to retrieve image URL. Only different is online article is getting website URL instead of local PC image path.

推荐答案

(this).css('background-image');
bg = bg.replace('url (','')。replace(')','')。replace(/ \/ gi,);
(this).css('background-image'); bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");



,它变得超级巨大我总共不能计算200k以上的字符数,我无法保存它。



我尝试了什么:



1.在线查看建议,但所有人都使用类似的方式来检索图片网址。唯一不同的是在线文章是获取网站URL而不是本地PC图像路径。


, it became super huge url with a total of 200k plus characters count where it is impossible for me to save it.

What I have tried:

1. Look online for suggestion but all are using similar way to retrieve image URL. Only different is online article is getting website URL instead of local PC image path.


经过一些网上研究,发现这是由于图像路径被转换为base64字符串。并且为了允许保存正确的路径,需要将base64字符串解码回正常的人类可读字符串。这可以通过使用atob()来实现。请参阅 Window atob()方法 [ ^ ]。谢谢Iw @ zi的帮助。愿这个建议能够帮助其他需要它的人。
After some research online, found that this is due to the image path get converted into base64 string. And in order to allow a proper path to be saved, the base64 string need to be decoded back into normal human readable string. This can be achieved by using atob(). Please refer to Window atob() Method[^]. Thank you Iw@zi for your help. May this suggestion will help other who need it.


这篇关于Html背景图片URL路径是巨大的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆