Nativescript with angular2,如何从受保护的服务器中检索图像
Nativescript with angular2, how to retrive a image from protected server
我正在使用 nativescript 和 angular2 创建一个简单的图像库,但图像在网络应用程序中受保护的服务器.. , 这是由 session cookie 解决的.. 但在这里我必须实现令牌身份验证.
我正在通过 :
下载图像
get(url: string) {
let headers = new Headers();
headers.append("Authorization", "Token " + Config.token);
return this.http.get(this.base_url+url, { headers: headers, })
}
在我的组件中:
this.mediaService.get(obj.url_tn)
.subscribe(data=>
{
let file:Blob = new Blob([data.arrayBuffer()]);
console.dump(data);
let urlFile = URL.createObjectURL(file );
this.photos.push(urlFile) ;
})
但这给了我一个错误:
Error: Error in :0:0 caused by: Blob is not defined
我不知道为什么将 headers 添加到 img
太难了,如何以简单的方式存档?
谢谢
这是方法,但不是 nativescript/angular2 方式,这是一个后端,使用 django..
import base64
import mimetypes
from django.http import FileResponse, HttpResponse
def download(request):
"""
your way to get your file and original_filename
"""
type, encoding = mimetypes.guess_type(original_filename)
if type is None:
type = 'application/octet-stream'
if request.GET.get('base64'):
response = HttpResponse("data:"+type+";base64,"+base64.b64encode(fobject.read()))
else:
response = FileResponse(fobject)
response['Content-Type'] = type
response['Content-Length'] = str(os.stat(file_path).st_size)
if encoding is not None:
response['Content-Encoding'] = encoding
"""
ETC ETC ETC
"""
return response
NativeScript 的 http
模块有一个名为 getImage(...)
.
的函数
我自己没试过,但我想你应该可以用它从你的私人服务中加载图片:
http://docs.nativescript.org/api-reference/modules/http.html#getimage
注意:getImage(...)
采用 URL
-字符串或 HttpRequestOptions
-对象。
我建议为它创建一个管道,就像我为图像缓存写的那样:
https://gist.github.com/m-abs/9e640e5805a21a842d55e02b291147c5
我正在使用 nativescript 和 angular2 创建一个简单的图像库,但图像在网络应用程序中受保护的服务器.. , 这是由 session cookie 解决的.. 但在这里我必须实现令牌身份验证.
我正在通过 :
下载图像 get(url: string) {
let headers = new Headers();
headers.append("Authorization", "Token " + Config.token);
return this.http.get(this.base_url+url, { headers: headers, })
}
在我的组件中:
this.mediaService.get(obj.url_tn)
.subscribe(data=>
{
let file:Blob = new Blob([data.arrayBuffer()]);
console.dump(data);
let urlFile = URL.createObjectURL(file );
this.photos.push(urlFile) ;
})
但这给了我一个错误:
Error: Error in :0:0 caused by: Blob is not defined
我不知道为什么将 headers 添加到 img
太难了,如何以简单的方式存档?
谢谢
这是方法,但不是 nativescript/angular2 方式,这是一个后端,使用 django..
import base64
import mimetypes
from django.http import FileResponse, HttpResponse
def download(request):
"""
your way to get your file and original_filename
"""
type, encoding = mimetypes.guess_type(original_filename)
if type is None:
type = 'application/octet-stream'
if request.GET.get('base64'):
response = HttpResponse("data:"+type+";base64,"+base64.b64encode(fobject.read()))
else:
response = FileResponse(fobject)
response['Content-Type'] = type
response['Content-Length'] = str(os.stat(file_path).st_size)
if encoding is not None:
response['Content-Encoding'] = encoding
"""
ETC ETC ETC
"""
return response
NativeScript 的 http
模块有一个名为 getImage(...)
.
我自己没试过,但我想你应该可以用它从你的私人服务中加载图片:
http://docs.nativescript.org/api-reference/modules/http.html#getimage
注意:getImage(...)
采用 URL
-字符串或 HttpRequestOptions
-对象。
我建议为它创建一个管道,就像我为图像缓存写的那样: https://gist.github.com/m-abs/9e640e5805a21a842d55e02b291147c5