Giter VIP home page Giter VIP logo

Comments (11)

pi0 avatar pi0 commented on August 10, 2024 1

does not work.

Indeed because $fetch already consumes res data as text(). @danielroe is working to improve blob handling. using fetch is more a workaround :)

Could it be because banner.data is a dataurl?

BTW do you mind sharing server/api/mc/banners.ts code? Might help to ensure changes fixes your usage properly.

from ofetch.

frbuceta avatar frbuceta commented on August 10, 2024 1

server/api/mc/banners.ts is connected to a database. As I was already creating an example, I paste the link here.

If I use fetch, the blob is empty. You can see it in the codesandbox that you paste above.

from ofetch.

frbuceta avatar frbuceta commented on August 10, 2024 1

The issue is beyond ohmyfetch then๐Ÿ˜… We also need to support Blob polyfill for Node.js (unjs/unenv#23) and probably some bits in h3 for response type (in your example you are returning a json array with base64 blob strings).

I would appreciate it if you can move the issue to nuxt/framework to track support.

I am going to create a new issue there that makes a link to the two issues

from ofetch.

danielroe avatar danielroe commented on August 10, 2024 1

@frbuceta Apart from Blob support in ohmyfetch, the issue you were experiencing is solved if you return a value from map.

Example.

<template>
  <pre>
    {{ banners }}
  </pre>
</template>

<script setup>
import _ from "lodash";

// eslint-disable-next-line
const { data: banners, refresh: bannersFetcher } = await useAsyncData(
  "banners",
  async () => {
    const banners = await $fetch("/api/banners").then((data) => {
      return Promise.all(
        _.map(data, async (banner) => {
          // const blob = await $fetch.raw(banner.data).then((res) => res.blob());
          const blob = await fetch(banner.data).then((r) => r.blob());
          console.log(blob);
+          return blob;
        })
      );
    });
    console.log(banners);
+    return banners;
  }
);
</script>

from ofetch.

danielroe avatar danielroe commented on August 10, 2024 1

File is not supported by Node: https://developer.mozilla.org/en-US/docs/Web/API/File

from ofetch.

pi0 avatar pi0 commented on August 10, 2024

Hi @frbuceta. $fetch already consumes and parses body and returns body (not res which can be accessed with $fetch.raw().then(res => res.blob)). To manually consume blob you might want to use fetch().then(r => r.blob()

from ofetch.

frbuceta avatar frbuceta commented on August 10, 2024

Hi @frbuceta. $fetch already consumes and parses body and returns body (not res which can be accessed with $fetch.raw().then(res => res.blob)). To manually consume blob you might want to use fetch().then(r => r.blob()

await $fetch.raw(banner.data).then(res => res.blob()) does not work. Could it be because banner.data is a dataurl?

from ofetch.

pi0 avatar pi0 commented on August 10, 2024

The issue is beyond ohmyfetch then๐Ÿ˜… We also need to support Blob polyfill for Node.js (unjs/unenv#23) and probably some bits in h3 for response type (in your example you are returning a json array with base64 blob strings).

I would appreciate it if you can move the issue to nuxt/framework to track support.

from ofetch.

pi0 avatar pi0 commented on August 10, 2024

BTW regarding example, for images you can directly reference them as :src (ie: parsing to blob is probably not necessary)

https://codesandbox.io/s/fetch-blob-error-forked-mh1fm?file=/app.vue

from ofetch.

frbuceta avatar frbuceta commented on August 10, 2024

BTW regarding example, for images you can directly reference them as :src (ie: parsing to blob is probably not necessary)

https://codesandbox.io/s/fetch-blob-error-forked-mh1fm?file=/app.vue

I need it to be in blob to send it to another service. It is not to render the image in html

from ofetch.

frbuceta avatar frbuceta commented on August 10, 2024

@frbuceta Apart from Blob support in ohmyfetch, the issue you were experiencing is solved if you return a value from map.

Example.

<template>
  <pre>
    {{ banners }}
  </pre>
</template>

<script setup>
import _ from "lodash";

// eslint-disable-next-line
const { data: banners, refresh: bannersFetcher } = await useAsyncData(
  "banners",
  async () => {
    const banners = await $fetch("/api/banners").then((data) => {
      return Promise.all(
        _.map(data, async (banner) => {
          // const blob = await $fetch.raw(banner.data).then((res) => res.blob());
          const blob = await fetch(banner.data).then((r) => r.blob());
          console.log(blob);
+          return blob;
        })
      );
    });
    console.log(banners);
+    return banners;
  }
);
</script>

I try to pass the Blob to File but it doesn't work either.

const { data: banners, refresh: bannersFetcher } = await useAsyncData(
  "banners",
  async () => {
    const banners = await $fetch("/api/banners").then((data) => {
      return Promise.all(
        _.map(data, async (banner) => {
          // const blob = await $fetch.raw(banner.data).then((res) => res.blob());
          const blob = await fetch(banner.data).then((r) => r.blob());
          console.log(blob);
          return { ...banner, file: new File([blob], banner.name) };
        })
      );
    });
    console.log(banners);
    return banners;
  }
);

from ofetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.