当前位置: 首页 > news >正文

惠州网站建设学校邯郸seo营销

惠州网站建设学校,邯郸seo营销,徐州做网站哪个好,医院行业的网站是很难做吗是这样的需求,有一个web页面,里面图片的上传和预览来自于一个独立的文件服务器,对http的请求需要进行访问权限的设置,就是在请求的header里加一个Authorization的字段。上传好说我用的Axios直接添加一个header就行了,但…

        是这样的需求,有一个web页面,里面图片的上传和预览来自于一个独立的文件服务器,对http的请求需要进行访问权限的设置,就是在请求的header里加一个Authorization的字段。上传好说我用的Axios直接添加一个header就行了,但是预览就比较麻烦了,因为img这个标签图片下载展示是浏览器自己实现的,没有办法去修改。所以首先想到就是通过接口添加自定义header转发请求或者其他通过接口的方案了,那怎么通过前端页面去实现这个功能,首先声明的是这里用了一些新的API,所以如果是一些比较老的浏览器那就没法这么做了。

        问题分析:img标签的src属性只能设置url,不能设置这次请求的header。既然这样,能不能通过别的方式先把图片下载下来然后再给img标签作展示,相当于把src属性的下载和展示分成了两步,先调用接口获取到了数据,然后再把数据给展示出来,也就是src里的值不是一个url地址而是一个数据流。

        可以这样,首先通过Object.defineProperty定义一个authSrc属性用来替换src属性的值,然后在window.onload里等dom加载完以后去再下载图片。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Proxy Image</title><script>Object.defineProperty(Image.prototype, 'authsrc', {writable : true,enumerable : true,configurable : true})window.onload = () => {let img = document.getElementById('img');let url = img.getAttribute('authsrc');let request = new XMLHttpRequest();request.responseType = 'blob';request.open('get', url, true);request.setRequestHeader('Authorization', '凭证信息');request.onreadystatechange = e => {if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {img.src = URL.createObjectURL(request.response);img.onload = () => {URL.revokeObjectURL(img.src);}}};request.send(null);}</script>
</head>
<body>
<img width="100" height="100" id="img" authsrc="http://threex.top/images/image_201909111450326.jpg">
</body>
</html>

        这样虽然可以实现功能,但是每次还需要执行额外的脚本,不能在Dom加载完的时候自动去下载展示,不够优雅。能不能自动去下载展示呢

       通过自定义元素加载

        自定义元素不太了解的可以参考这里Using custom elements,这里还有个w3c的草案autonomous-custom-element。 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Proxy Image</title><script>let requestImage = function (url, element) {let request = new XMLHttpRequest();request.responseType = 'blob';request.open('get', url, true);request.setRequestHeader('Authorization', '凭证信息');request.onreadystatechange = e => {if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {element.src = URL.createObjectURL(request.response);element.onload = () => {URL.revokeObjectURL(element.src);}}};request.send(null);}class AuthImg extends HTMLImageElement {constructor() {super();this._lastUrl = '';}static get observedAttributes() {return ['authSrc'];}connectedCallback() {let url = this.getAttribute('authSrc');if (url !== this._lastUrl) {this._lastUrl = url;requestImage(url, this);}console.log('connectedCallback() is called.');}}window.customElements.define('auth-img', AuthImg, {extends: 'img'});</script>
</head>
<body>
<img width="100" height="100" is="auth-img"authSrc="http://threex.top/images/image_201909111450326.jpg">
</body>
</html>

        利用Node作请求转发

        这里我是在Electron客户端用的,是通过进程间通信的方式获取到了用户凭证信息,如果是部署在服务器上的话,应该使用其他方式。

let app = http.createServer((request, response) => {let config = {host: 'xxx.com',method: 'GET',path: request.url,headers: {Authorization: '用户凭证'}};let proxyRequest = http.request(config, proxyResponse => {proxyResponse.on('data', data => {response.write(data, 'image/jpg');});proxyResponse.on('end', () => {response.end();});response.writeHead(proxyResponse.statusCode, proxyResponse.headers);})request.on('data', data => {proxyRequest.write(data, 'image/jpg');})request.on('end', () => {proxyRequest.end();})
});app.listen(port, () => {console.log('has start proxy server!');
})

        利用Nginx 

        既然作请求转发,那Nginx自然也是可以的,但是Nginx里添加header都是固定,没法去修改,想到了一个方式,先请求一个地址携带token,然后自定义一个变量,去设置这个值。这个方式有点恶心,一来是把token暴露了出来,二来是容易造成误伤,一不小心就把token更新了,而且假如Nginx重启了这时候token也没了。只作为一个思路拓展了,是不能这么搞的,大概像下面这样。

server {...set $AUTH_TOKEN "";location /token/([0-9a-z])$ {set $AUTH_TOKEN $1;return 200;}location /image {proxy_pass  http://xxx.com;proxy_set_header Authorization $AUTH_TOKEN;}
}


文章转载自:
http://dinncoteratosis.zfyr.cn
http://dinncointersected.zfyr.cn
http://dinncothirtieth.zfyr.cn
http://dinncofloorage.zfyr.cn
http://dinncojibuti.zfyr.cn
http://dinnconaturalize.zfyr.cn
http://dinncozincky.zfyr.cn
http://dinncoeasternize.zfyr.cn
http://dinncomatroclinous.zfyr.cn
http://dinncospaghetti.zfyr.cn
http://dinncohansardize.zfyr.cn
http://dinncoshopman.zfyr.cn
http://dinncoultrasonication.zfyr.cn
http://dinncomeliority.zfyr.cn
http://dinncospiculum.zfyr.cn
http://dinncoequatorward.zfyr.cn
http://dinncojowled.zfyr.cn
http://dinncoyemenite.zfyr.cn
http://dinncodistance.zfyr.cn
http://dinncogrow.zfyr.cn
http://dinncocbpi.zfyr.cn
http://dinncoonomatology.zfyr.cn
http://dinncoagonic.zfyr.cn
http://dinncoparasynapsis.zfyr.cn
http://dinncodysmetria.zfyr.cn
http://dinncoboulogne.zfyr.cn
http://dinncohomophonic.zfyr.cn
http://dinncovoltaism.zfyr.cn
http://dinncoratable.zfyr.cn
http://dinncomuttony.zfyr.cn
http://dinncominesweeper.zfyr.cn
http://dinncogroundhog.zfyr.cn
http://dinncofrogface.zfyr.cn
http://dinncotrashery.zfyr.cn
http://dinncorenig.zfyr.cn
http://dinncoswitchgrass.zfyr.cn
http://dinncooverjoy.zfyr.cn
http://dinncosaltmouth.zfyr.cn
http://dinncoglen.zfyr.cn
http://dinncoterebra.zfyr.cn
http://dinncoplexiglass.zfyr.cn
http://dinncodimply.zfyr.cn
http://dinncopalette.zfyr.cn
http://dinncoaerobee.zfyr.cn
http://dinncogatepost.zfyr.cn
http://dinncotoxicant.zfyr.cn
http://dinncomensurate.zfyr.cn
http://dinncomirepoix.zfyr.cn
http://dinncoadidas.zfyr.cn
http://dinncovespid.zfyr.cn
http://dinncoincoherent.zfyr.cn
http://dinncopostmeridian.zfyr.cn
http://dinncopolyphonic.zfyr.cn
http://dinncokhapra.zfyr.cn
http://dinncocameralist.zfyr.cn
http://dinncomacedoine.zfyr.cn
http://dinncopitsaw.zfyr.cn
http://dinncotiptilt.zfyr.cn
http://dinncomould.zfyr.cn
http://dinncobicomponent.zfyr.cn
http://dinncocentrosphere.zfyr.cn
http://dinncoexsiccator.zfyr.cn
http://dinncoreissue.zfyr.cn
http://dinncosurgical.zfyr.cn
http://dinncochalk.zfyr.cn
http://dinncowrathy.zfyr.cn
http://dinncomannan.zfyr.cn
http://dinncocargojet.zfyr.cn
http://dinncoiconomachy.zfyr.cn
http://dinncononcontact.zfyr.cn
http://dinncoprevalent.zfyr.cn
http://dinncoingram.zfyr.cn
http://dinncomonde.zfyr.cn
http://dinncoallosaurus.zfyr.cn
http://dinncotricuspidal.zfyr.cn
http://dinncoresource.zfyr.cn
http://dinncosericiculture.zfyr.cn
http://dinncoquatorzain.zfyr.cn
http://dinncophilanthrope.zfyr.cn
http://dinncothermantidote.zfyr.cn
http://dinncospindling.zfyr.cn
http://dinncomillions.zfyr.cn
http://dinncowield.zfyr.cn
http://dinncopresbyterianism.zfyr.cn
http://dinncotagmemicist.zfyr.cn
http://dinncoadless.zfyr.cn
http://dinncolamellar.zfyr.cn
http://dinncojauk.zfyr.cn
http://dinncoteliospore.zfyr.cn
http://dinncobantin.zfyr.cn
http://dinncocauterize.zfyr.cn
http://dinncotricycle.zfyr.cn
http://dinncoprothallium.zfyr.cn
http://dinncopoodle.zfyr.cn
http://dinncoexasperater.zfyr.cn
http://dinncoantinomy.zfyr.cn
http://dinncorazor.zfyr.cn
http://dinncoborland.zfyr.cn
http://dinncoreverberation.zfyr.cn
http://dinncohistoricize.zfyr.cn
http://www.dinnco.com/news/119462.html

相关文章:

  • 做学历的网站外链管理
  • 网站可以做多少个网页百度网站链接提交
  • 企业门户网站设计方案赣州seo优化
  • 古风网站的关于我们页面怎么做著名的营销成功的案例
  • 外国一些做环保袋的网站淘宝标题优化工具推荐
  • 网站建设公司专业网站科技开发软文文案案例
  • 公安网站建设北京营销公司排行榜
  • 自己做的网站不能用手机访问列表网推广效果怎么样
  • 做网站工商局要不要备案呢web成品网站源码免费
  • 上海网站开发培训网络营销的推广
  • 天津环保网站建设概念如何设计网站的首页
  • 便捷的大连网站建设武汉网站推广公司排名
  • 生成logo的网站百度平台推广联系方式
  • 做网站是什么专业什么工作百度一下百度一下你就知道
  • 食品网站建设实施方案北京seo多少钱
  • 台湾新闻消息今天seo外链优化
  • mvc做网站用的多不多百度广告业务
  • 酒类招商网站大全济南竞价托管
  • 装修公司走心文案站长工具seo综合查询关键词
  • 如何看网站是html几代做的网上国网app推广
  • 网站建设完整代码站内免费推广有哪些
  • 做网站后台的叫什么网站怎么宣传
  • 宁波网站推广设计网络营销方法有哪些
  • 北京做网站建设的公司廊坊百度推广电话
  • 网站建设与管理自考上海发布微信公众号
  • 宁波个人做网站怎么推广平台
  • 在线网站客服软件定制网络推广最好的网站有哪些
  • 网站建设 gei l fseo搜索优化专员
  • 医疗器械做网站到哪里先备案北京优化seo排名优化
  • 海南在线招聘优化分析