How to get past null characters in a file posted to an asp.net form?

260 views Asked by At

I have an asp.net MVC application that takes in uploaded NMEA track files from small GPS loggers. In some cases, the loggers will inject null (0x0) values into the track file text. Is there a way to strip out these 0x0 characters from the HttpPostedFile's InputStream before saving the file to the server's file system for processing? As it is, I can only get to the first null character and that's all of the file that gets saved on the server.

Thanks, Matthew

2

There are 2 answers

1
queen3 On

InputStream is binary and shouldn't care about zeros. So, the problem is with the data conversion to text - but you didn't show the code that you use to convert.

You may manually skip/replaces zeros:

var outstream = new StringStream();
var b;
while ((b = http.InputStream.ReadByte()) != -1)
  if (b != 0)
      outstream.Write(b);

and now you got stream without zeros (of course you can read in blocks, etc, if you want to).

0
queen3 On

If you use SaveAs, it should write zeros. Can you check the InputStream content in debugger (for example using my reading method above) - are you sure that HttpPostedFile does contain zeros - that is, that you receive data correctly? Do you use ENCTYPE="multipart/form-data" for your upload form?