搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

Learn More

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