我想将文件上传到Firebase托管站点。我已经阅读了文档,但是我无法编写代码来提出HTTP请求。
参考:
https://firebase.google.com/support#upload-fload-fload-fload-fload-flo >
我有两个问题。
- 当我查看上传文件的示例,它显示
content content -file2.gz
在请求正文中。这是否意味着当我上传它时,我将文件内容包括在请求的正文中,还是必须将文件作为对象在请求正文中包含?
- 如果可能的话,请给我代码以完成它,或者让我知道我应该在标题,身体等上写些什么。我使用的是Ionic 6。下面是我的尝试之一,但它不起作用。
构造函数(私有http:httpclient){
令header = new httpheaders();
让令牌=“ oauth2访问令牌”;
header.set(“授权”,“持有人” +令牌);
const h = {'content-type':'application/offer-stream',“授权”:“ bearer” + soken}
header.set(“ content-type”,“ application/json”);
this.getjson()。订阅(data => {
console.log(“ file:”,data);
http.post(
“ https://upload-firebasehosting.googleapis.com/upload/sites/siteid/siteid/versions/versionid/files/files/filehash”,
数据,
{
标题:h
}
).subscribe(data1 => {
console.log(data1);
}))
}))
}
public getjson():可观察< any> {
返回this.http.get(“ ./ Assets/file.json”,{
观察:“身体”,
响应类型:“ blob” //此处告诉httpclient将其解析为文本,而不是json
});
}
如果我错误地问,请纠正我。谢谢。
I will like to upload files to a firebase hosting site. I have read through the documentation but I'm not able to write the code to make the http request.
Reference:
https://firebase.google.com/docs/reference/hosting/rest/v1beta1/sites.versions/populateFiles
https://firebase.google.com/support#upload-files
I have two questions.
- When I'm looking at the example of upload files, it shows
content-of-file2.gz
in the body of the request. Does it means when I upload it, I include the content of file in the body of the request or I have to include the file as an object in the body of the request?
- If possible, please provide me the code to accomplish it or let me know what should I write in the header, body etc. I'm using Ionic 6. Below is one of my attempts, but it does not work.
constructor(private http: HttpClient) {
let header = new HttpHeaders();
let token = "OAuth2 access token";
header.set("Authorization", "Bearer " + token);
const h = { 'Content-Type': 'application/octet-stream', "Authorization": "Bearer " + token }
header.set("Content-Type", "application/json");
this.getJson().subscribe(data => {
console.log("File:", data);
http.post(
"https://upload-firebasehosting.googleapis.com/upload/sites/siteId/versions/versionId/files/fileHash",
data,
{
headers: h
}
).subscribe(data1 => {
console.log(data1);
})
})
}
public getJson(): Observable<any> {
return this.http.get("./assets/file.json", {
observe: 'body',
responseType: "blob" // This one here tells HttpClient to parse it as text, not as JSON
});
}
Please correct me if I asked wrongly. Thanks.
发布评论