我正在使用 Ionic Native Camera 插件拍照并将其保存在 base64 中。
然后我想将该图像发送到 Amazon s3 进行存储。
我通过 POST 向我的 API 发送一个包,其中包含 base64 图像、桶名、键名等。
在 http 将数据发送到我的服务器之前,我在控制台中收到一条警告:“警告:清理不安全的 URL 值数据:图像/jpeg;base64”
我做了一些研究,发现 Angular 会清理 url,因为它们可能包含对接收者有害的 XSS。
因为我知道我的 URL 是干净的,所以我在发送之前导入了 DomSanitizer 并在图像上使用了“bypassSecurityTrustUrl”。
我仍然收到与以前相同的错误消息。
是否有针对出站 HTTP 帖子禁用此功能的方法?我仍然想保留它用于传入数据。
这是图像服务代码...
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { DomSanitizer } from '@angular/platform-browser';
@Injectable()
export class ImageService {
constructor(
private http: Http,
private sanitizer: DomSanitizer
) {}
uploadProfileImage(userInfo, image) {
return new Promise(resolve => {
let pkg = {
image: this.sanitizer.bypassSecurityTrustUrl(image),
name: userInfo.uid,
folder: 'profileImages',
email: userInfo.email
}
console.log('pkg upload profile image', JSON.stringify(pkg));
this.http.post('https://myurl/api/uploadpicture', pkg)
.subscribe( res => console.log('response from upload picture', JSON.stringify(res)));
})
}
}
当我控制台记录“pkg upload profile image”时,图像值如下所示
{"image":{"changingThisBreaksApplicationSecurity":"data:image/jpeg;base64,file:///var/mobile/Containers/Data/Application/xxxxxxxxxx/tmp/cdv_photo_007.jpg"},
我从 Amazon S3 得到的响应很好,文件以正确的 key 名称发布到正确的存储桶中,但图像已损坏且只有约 80 字节。
我非常感谢您的帮助,这在我使用 Angular 1.x 的 Ionic 1 应用程序上运行良好。
谢谢
最佳答案
我想通了。
我没有在 Cordova Camera getPicture 中添加任何选项。
我加了
let options = {
quality : 75,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 300,
targetHeight: 300,
saveToPhotoAlbum: false
};
现在它工作正常。
当我没有传递选项时,“图像”只是我的临时相机胶卷文件夹 (ios) 的 url。当我传递选项时,“图像”是 base64,我不再收到警告。
关于angular - Ionic 2 Angular 2 HTTP 将 Base64 图像发布到服务器导致在发送之前清理不安全的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41382931/