开发者

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

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 t开发者_如何学编程hat's all of the file that gets saved on the server.

Thanks, Matthew


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).


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜