0

I've seen this post:  characters appended to the begining of each file.

In that case, the author was manually reading the source file and writing the contents. In my case, I'm abstracting it away via HttpRequest.TransmitFile():

public void ProcessRequest(HttpContext context)
{
    HttpRequest req = context.Request;
    HttpResponse resp = context.Response;

    resp.ContentType = "application/javascript";

    resp.TransmitFile("foo.js");
    resp.TransmitFile("bar.js");
    resp.TransmitFile("baz.js");
}

The .js files are indeed encoded in UTF-8. This means the  BOM incorrectly appears at the beginning of each but the first file.

The nice things about TransmitFile() are that (a) it abstracts away the entire reading+writing process, and (b) it's optimized to not read the files into memory first -- which is hugely important when the files are large and/or you have many concurrent requests. But the flip side is that I'm not able to re-encode it into UTF-8 without the BOM. (I guess this is an example of a leaky abstraction.)

Is there any elegant way to solve this problem? Thanks!

Community
  • 1
  • 1
Aseem Kishore
  • 10,404
  • 10
  • 51
  • 56
  • Is this actually a problem in practice? The BOM is a whitespace character (zero-width nonbreaking space, code point U+FFFE). Is the JS interpreter choking on it? – Jeffrey Hantin Sep 04 '09 at 00:10
  • Most importantly, IE7 and below choke on it (invalid character). Less important but still annoying, JSLint chokes on it. – Aseem Kishore Sep 04 '09 at 18:01
  • I don't think TransmitFile actually does any magic optimizations for transmitting, nothing you couldn't recreate with stream read/write pumps – meandmycode Sep 06 '09 at 14:17
  • @meandmycode sorry for the late reply, but yes you're right _if_ you read/write the raw bytes. If you read/write the text contents as strings, you don't see this issue. – Aseem Kishore Mar 20 '11 at 06:49

1 Answers1

2

Closing the loop on this: TransmitFile() transmits the raw bytes of the file, so if the files are encoded in UTF-8 with a BOM and you transmit multiple files, you'll transmit multiple BOMs.

If you read the files yourself into memory as strings and transmit those strings, you won't get this issue. We ended up going with simply encoding the files as UTF-8 without the BOM or as ANSI.

Aseem Kishore
  • 10,404
  • 10
  • 51
  • 56