How to convert a file into byte array in memory?
As opposed to saving the data as a string (which allocates more memory than needed and might not work if the binary data has null bytes in it), I would recommend an approach more like
foreach (var file in files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
var fileBytes = ms.ToArray();
string s = Convert.ToBase64String(fileBytes);
// act on the Base64 data
}
}
}
Also, for the benefit of others, the source code for IFormFile
can be found on GitHub
C# read IFormFile into byte[]
You can't open an IFormFile
the same way you would a file on disk. You'll have to use IFormFile.OpenReadStream()
instead. Docs here
public async Task<ActionResult> UploadDocument([FromForm]DataWrapper data)
{
IFormFile file = data.File;
long length = file.Length;
if (length < 0)
return BadRequest();
using var fileStream = file.OpenReadStream();
byte[] bytes = new byte[length];
fileStream.Read(bytes, 0, (int)file.Length);
}
The reason that fileStream.Read(bytes, 0, (int)file.Length);
appears to be empty is, because it is. The IFormFile.Filename
is the name of the file given by the request and doesn't exist on disk.
Converting a 3GB file into a byte array
Use InputStream
and OutputStream
.
They are meant for big amounts of data, for example, when you work with 3GB of data and can't load it into memory.
If you are trying to upload a file, use FileInputStream
. Create a File
object, pass it to the FileOutputStream
constructor and start reading bytes from its InputStream
to a byte[]
buffer and send the bytes with an OutputStream
to the server.
This approach will not cause an OutOfMemoryError
,because you are reading only enough bytes to fill up the buffer, which should be about 2KB - 8KB in size. After the buffer is full, you write the bytes to the server. After the buffer was written to the server, you read into the buffer again, and the process keeps going on until the whole file is uploaded.
Example using FileInputStream
File file = new File("yourfile.txt");
FileInputStream fis = null;
OutputStream outputStream = null;
url = new URL(desiredUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
fis = new FileInputStream(file);
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
int actuallyRead;
byte[] buffer = new byte[2048];
while ((actuallyRead = fis.read(buffer)) != -1) {
//do something with bytes, for example, write to the server
outputStream.write(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Note: This approach does not mean you need to reconnect to the server
each time a buffer is filled. It will continuously write to the
server, until it is done processing the file. This will happen all
under the same, single connection.
Reliable way to convert a file to a byte[]
byte[] bytes = System.IO.File.ReadAllBytes(filename);
That should do the trick. ReadAllBytes opens the file, reads its contents into a new byte array, then closes it. Here's the MSDN page for that method.
Best way to read a large file into a byte array in C#?
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. You should do that in chunks.
Convert file to byte array and vice versa
I think you misunderstood what the java.io.File
class really represents. It is just a representation of the file on your system, i.e. its name, its path etc.
Did you even look at the Javadoc for the java.io.File
class? Have a look here
If you check the fields it has or the methods or constructor arguments, you immediately get the hint that all it is, is a representation of the URL/path.
Oracle provides quite an extensive tutorial in their Java File I/O tutorial, with the latest NIO.2 functionality too.
With NIO.2 you can read it in one line using java.nio.file.Files.readAllBytes().
Similarly you can use java.nio.file.Files.write() to write all bytes in your byte array.
UPDATE
Since the question is tagged Android, the more conventional way is to wrap the FileInputStream
in a BufferedInputStream
and then wrap that in a ByteArrayInputStream
.
That will allow you to read the contents in a byte[]
. Similarly the counterparts to them exist for the OutputStream
.
File to byte[] in Java
It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here FileUtils.readFileToByteArray(File input)
.
Can I convert a file to a byte array then save the byte array to a file keeping its properties?
name, attributes, timestamps, etc are all meta data of the file and not part of the file contents.. so you need a container format.. You could use XML, or MIME messages, or any encapsulation scheme you desire.
Converting file to byte array slowing down the app
Doing I/O on the main application thread will freeze your UI for the duration of that I/O, meaning that your UI will be unresponsive. Moving I/O to a background thread is necessary for a well-behaved UI.
Android's insistence that we can only safely update the UI from the main application thread means that you need to use an AsyncTask
, a Thread
and stuff like runOnUiThread()
, RxJava/RxAndroid, or other approaches to arrange both the background work and updating the UI when that background work is done.
In your case, doing the convert()
logic on a background thread, and updating the waveView
on the main application thread, should help.
Related Topics
How to Highlight Wrapped Text in a Control Using the Graphics
Httpcontext.Current Is Null When Unit Test
Extract Thumbnail for Any File in Windows
Auto-Resize Multiple Forms Rendered on Panel
What Does Linq Return When the Results Are Empty
How to Use Async to Increase Winforms Performance
How to Compare Lists in Unit Testing
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
How to Create "Embedded" SQL 2008 Database File If It Doesn't Exist
Combining Datatemplates at Runtime
Convert String to System.Io.Stream
How to Change the Button Text for 'Yes' and 'No' Buttons in the Messagebox.Show Dialog
How to Know the Selected Checkboxes from Within the Httppost Create Action Method
Dynamically Switch Wcf Web Service Reference Url Path Through Config File
How to Open a Web Page from My Application
Setting Culture for ASP.NET MVC Application on VS Dev Server and Iis