如何在没有路径的情况下直接将文件转换为字节数组(不保存文件)

时间:2020-12-27 21:43:25

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.

然后我将此代码上传到我的nosql数据库。这一切都运行正常,但问题是我不想保存文件。而不是我想直接将文件上传到我的数据库。如果我可以直接将文件转换为字节数组而不保存它是可能的。

 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
}

2 个解决方案

#1


40  

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, IFormFile is documented on docs.asp.net with the source code on GitHub

此外,为了其他人的利益,IFormFile记录在docs.asp.net上,源代码在GitHub上

Edit

编辑

Per the suggestions, I tweaked the example to use the CopyTo method of the IFormFile interface

根据建议,我调整了示例以使用IFormFile接口的CopyTo方法

#2


2  

You can use the following code to convert it to a byte array:

您可以使用以下代码将其转换为字节数组:

foreach (var file in files)
{
   if (file.Length > 0)
    {
      var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
      using (var reader = new StreamReader(file.OpenReadStream()))
      {
        string contentAsString = reader.ReadToEnd();
        byte[] bytes = new byte[contentAsString.Length * sizeof(char)];
        System.Buffer.BlockCopy(contentAsString.ToCharArray(), 0, bytes, 0, bytes.Length);
      }
   }
}

#1


40  

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, IFormFile is documented on docs.asp.net with the source code on GitHub

此外,为了其他人的利益,IFormFile记录在docs.asp.net上,源代码在GitHub上

Edit

编辑

Per the suggestions, I tweaked the example to use the CopyTo method of the IFormFile interface

根据建议,我调整了示例以使用IFormFile接口的CopyTo方法

#2


2  

You can use the following code to convert it to a byte array:

您可以使用以下代码将其转换为字节数组:

foreach (var file in files)
{
   if (file.Length > 0)
    {
      var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
      using (var reader = new StreamReader(file.OpenReadStream()))
      {
        string contentAsString = reader.ReadToEnd();
        byte[] bytes = new byte[contentAsString.Length * sizeof(char)];
        System.Buffer.BlockCopy(contentAsString.ToCharArray(), 0, bytes, 0, bytes.Length);
      }
   }
}