Шукати в статтях підтримки

Остерігайтеся нападів зловмисників. Mozilla ніколи не просить вас зателефонувати, надіслати номер телефону у повідомленні або поділитися з кимось особистими даними. Будь ласка, повідомте про підозрілі дії за допомогою меню “Повідомити про зловживання”

Докладніше

Ця тема перенесена в архів. Якщо вам потрібна допомога, запитайте.

Image fetch() fails when content-type charset is utf-8

  • 5 відповідей
  • 1 має цю проблему
  • 11 переглядів
  • Остання відповідь від donmccurdy

more options

I'm using the `http-server` module from npm to serve a page locally. This module always includes the encoding=utf-8 header in content type, even for images. When requesting a JPG as follows:

<img src="puppy.png">

... it works fine. When trying to load the image via fetch():

  fetch( 'public/puppy.jpg' )
    .then( ( response ) => response.blob() )
    .then( ( blob ) => self.createImageBitmap( blob ) )
    .then( ( imageBitmap ) => {

      const canvasEl = document.querySelector( '#canvas' );
      const ctx = canvasEl.getContext( '2d' );
      ctx.drawImage( imageBitmap, 0, 0 );

    } )
    .catch( function ( error ) {

      console.warn( 'Error: ', error );

    } );

I see:

Error: DOMException [InvalidStateError: "An attempt was made to use an object that is not, or is no longer, usable"

In Chrome, both methods work. No other browsers currently support ImageBitmap.

I think the `http-server` module is technically incorrect in using encoding=utf-8, and when I switch to another serving module that doesn't, this error goes away. Nevertheless, i was surprised that Firefox was inconsistent, so here's a bug report. :)

I'm using the `http-server` module from npm to serve a page locally. This module always includes the encoding=utf-8 header in content type, even for images. When requesting a JPG as follows: &lt;img src="puppy.png"&gt; ... it works fine. When trying to load the image via fetch(): <pre><nowiki> fetch( 'public/puppy.jpg' ) .then( ( response ) => response.blob() ) .then( ( blob ) => self.createImageBitmap( blob ) ) .then( ( imageBitmap ) => { const canvasEl = document.querySelector( '#canvas' ); const ctx = canvasEl.getContext( '2d' ); ctx.drawImage( imageBitmap, 0, 0 ); } ) .catch( function ( error ) { console.warn( 'Error: ', error ); } ); </nowiki></pre> I see: Error: DOMException [InvalidStateError: "An attempt was made to use an object that is not, or is no longer, usable" In Chrome, both methods work. No other browsers currently support ImageBitmap. I think the `http-server` module is technically incorrect in using encoding=utf-8, and when I switch to another serving module that doesn't, this error goes away. Nevertheless, i was surprised that Firefox was inconsistent, so here's a bug report. :)

Змінено cor-el

Усі відповіді (5)

more options

Oops. The line below "When requesting a JPG as follows:" should have been HTML showing an "img" tag with a "src="public/puppy.png"" attribute. That got stripped.

more options

What content-type is being sent by the server? I would think image/png would cause Firefox to ignore a character encoding header.

You should be able to confirm using the Network Monitor.

https://developer.mozilla.org/docs/Tools/Network_Monitor

more options

Everything works when the server sends "image/jpeg", but not with "image/jpeg; charset=utf-8". Full response headers attached, both for my working and broken server. The broken server is the one beginning with "server: ecstatic".

Also plausible that it's one of the other headers, but charset looked like the probable culprit.

more options

Do you want to file a bug?

https://bugzilla.mozilla.org/

more options