添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
打酱油的柿子  ·  Perl 数组 | 菜鸟教程·  3 月前    · 
善良的小蝌蚪  ·  获取错误: /bin/sh ...·  1 年前    · 
爱喝酒的橙子  ·  vue插件总结 - 知乎·  1 年前    · 
闯红灯的蛋挞  ·  syncfusion ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
 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
                When you originally saved the file, what form was it in? However you had it in memory, it should have already been a byte array, or convertible to a byte array. We would need to see how you are obtaining the file in the first place, and how you are saving it.
– Kevin
                Apr 5, 2016 at 16:33

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

you could shorten the code by using the copy functions on IFormFile themselve. using (var stream = new MemoryStream()) { formFile.CopyTo(stream); return stream.ToArray(); } – Bosken85 Sep 19, 2017 at 9:26 Any way I can copy only the first 20 bytes or so to the memory stream and byte array? Only need those to check the file signature – kanpeki Oct 18, 2019 at 13:12 Something to the effect of the following should help you out. using (var stream = file.OpenReadStream()) { var buffer = new byte[20]; stream.Read(buffer, 0, 20); } – erdomke Oct 18, 2019 at 17:02
public static class FormFileExtensions
    public static async Task<byte[]> GetBytes(this IFormFile formFile)
        await using var memoryStream = new MemoryStream();
        await formFile.CopyToAsync(memoryStream);
        return memoryStream.ToArray();

Usage

var bytes = await formFile.GetBytes();
var hexString = Convert.ToBase64String(bytes);
                Let's change method name to GetBytesAsync(), so this suffix will indicate this is async function
– Mahmood Garibov
                Jun 25, 2022 at 16:58
                @MahmoodGaribov this won't happen as I don't see a point to add the suffix.  See also: docs.particular.net/nservicebus/upgrades/5to6/…
– Rookian
                Jul 1, 2022 at 9:14
   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);

You can retrieve your file by using the Request.Form already implemented (as image for instance) :

var bytes = new byte[Request.Form.Files.First().Length];
var hexString = Convert.ToBase64String(bytes);

Hope it help

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.