Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a file into byte array in memory?

Here is my code:

 public async Task<IActionResult> Index(ICollection<IFormFile> files)  {     foreach (var file in files)         uploaddb(file);         var uploads = Path.Combine(_environment.WebRootPath, "uploads");     foreach (var file in files)     {         if (file.Length > 0)         {             var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');              await file.SaveAsAsync(Path.Combine(uploads, fileName));         }     } } 

Now I am converting this file into byte array using this code:

var filepath = Path.Combine(_environment.WebRootPath, "uploads/Book1.xlsx"); byte[] fileBytes = System.IO.File.ReadAllBytes(filepath); string s = Convert.ToBase64String(fileBytes); 

And then I am uploading this code into my nosql database.This is all working fine but the problem is i don't want to save the file. Instead of that i want to directly upload the file into my database. And it can be possible if i can just convert the file into byte array directly without saving it.

public async Task<IActionResult> Index(ICollection<IFormFile> files) {     foreach (var file in files)         uploaddb(file);        var uploads = Path.Combine(_environment.WebRootPath, "uploads");     foreach (var file in files)     {         if (file.Length > 0)         {             var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');  ///Code to Convert the file into byte array } 
like image 481
Kalp Avatar asked Apr 05 '16 16:04

Kalp


People also ask

How do I turn a object into a byte array?

Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class. Flush the contents to the stream using the flush() method. Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.

What is byte array?

A byte array is simply a collection of bytes. The bytearray() method returns a bytearray object, which is an array of the specified bytes. The bytearray class is a mutable array of numbers ranging from 0 to 256.


2 Answers

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

like image 92
erdomke Avatar answered Oct 07 '22 15:10

erdomke


You can just write a simple extension:

public static class FormFileExtensions {     public static async Task<byte[]> GetBytes(this IFormFile formFile)     {         using (var memoryStream = new MemoryStream())         {             await formFile.CopyToAsync(memoryStream);             return memoryStream.ToArray();         }     } } 

Usage

var bytes = await formFile.GetBytes(); var hexString = Convert.ToBase64String(bytes); 
like image 45
Rookian Avatar answered Oct 07 '22 15:10

Rookian