如何编写HTTP请求代码以将文件上传到Firebase托管

发布于 2025-02-10 23:54:47 字数 1694 浏览 1 评论 0 原文

我想将文件上传到Firebase托管站点。我已经阅读了文档,但是我无法编写代码来提出HTTP请求。

参考:

https://firebase.google.com/support#upload-fload-fload-fload-fload-flo >

我有两个问题。

  1. 当我查看上传文件的示例,它显示 content content -file2.gz 在请求正文中。这是否意味着当我上传它时,我将文件内容包括在请求的正文中,还是必须将文件作为对象在请求正文中包含?
  2. 如果可能的话,请给我代码以完成它,或者让我知道我应该在标题,身体等上写些什么。我使用的是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.

  1. 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?
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文