Read a file into a byte array


















Read ; System. FileInfo fileName. ReadBytes Int32 byteLength ; fs. Close ; fs. Dispose ; binaryReader. Clear ; Response. ClearContent ; Response. ClearHeaders ; Response. Active 1 year, 7 months ago. Viewed k times. Open, FileAccess. Peter Mortensen ReadAllBytes fileName. Why does it being a third party webservice imply the file needs to be fully in RAM before being sent to the webservice, rather than streamed?

The webservice won't know the difference. Brian, Some clients don't know how to handle a. NET stream, like Java for instance. When this is the case all that can be done is to read the entire file in byte array. NET stream. The clients won't know the difference either way. Add a comment. Active Oldest Votes. Simply replace the whole thing with: return File. ReadAllBytes fileName ; However, if you are concerned about the memory consumption, you should not read the whole file into memory all at once at all.

Generally, you should stream the file and not store it in memory altogether. You might want to look at this for a stopgap measure: msdn. There is a limit for array size in.

NET, but in. NET 4. This should not be the accepted or top-rated answer for a large file read, at least the code given. The statement "you should not read the whole file into memory all at once at all. You should do that in chunks" is correct and should have been backed by code.

Downvoting until that part is rectified, as this answer's code is very misleading and contradictory to that very correct statement. Show 5 more comments. Read buffer, 0, buffer. Marc Gravell Marc Gravell k gold badges silver badges bronze badges. To add to your statement, I even suggest considering async ASP. However, if you have to read the whole file to a byte[] for some reason, I suggest avoid using streams or anything else and just use the system provided API.

Mehrdad - agreed; but the full context isn't clear. Likewise MVC has action-results for this. Yes I need all the data at once. It's going to a third party webservice. Tony: I stated in my answer: File. Length; while bytesRead - source.

ReadAllBytes fileName ;. Powerlord Powerlord Note that this can stall when getting really large files. Your code can be factored to this in lieu of File. Length]; fs. Read buffer, 0, int fs. Also note that the last argument to the FileStream is a buffer size. As always a simple sample program to profile which is fastest will be most beneficial. Why would type of hardware make a difference?

So if it's IDE you use some. There are different types of hard disk drives. Length Dim buffer As Byte. Try fs. Read buffer, 0, filelen Catch ex As Exception Trace. Message End Try fs. Read buffer, 0, filelen. When I compile it I get a warning: Variable 'buffer' is used before it has been assigned a value.

A null reference exception could result at runtime. So how do I initialize " buffer "? Or is there a better way to do this? You are missing this. Dim buff fs. Length As Byte. Also keep in mind if the file size is big, MB or greater you may want to read it in in chunks. Also put a try catch around it in case things hang. If fi. Exists AndAlso fileName. Open fpath, IO.



0コメント

  • 1000 / 1000