0

The non-public.member _baseStream attribute in IFormFile in my ASP.NET Core application throws the following exception after uploading a file:

ReadTimeout =   ((Microsoft.AspNetCore.Http.Internal.FormFile)BildUpload)._baseStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

I'm trying to upload a file using a razor page with the following code:

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <input type="file" name="BildUpload"  />
    </div>
    <input type="submit" value="Upload" class="btn btn-default" />
</form>

In the codebehind class I only got the declaration and nothing else read or writes the paramter excepts the razor page:

    public IFormFile BildUpload { get; set; }

Thanks for your help!

My final goal is to parse the file to a byte array and sava at to a database like this: How to Convert a file into byte array directly without its path(Without saving file) But over there I'm getting a nullpointer exception.

pschlz
  • 173
  • 1
  • 12
  • 2
    That sounds like an XY problem. Why would this exception be a problem? Why are you trying to get the read timeout on an already read HTTP request body? – CodeCaster Oct 09 '18 at 09:43
  • My goal is to parse the file to a byte array and save it to the database. I can't do this because of this exception. By parsing I get a null pointer exception. I'm trying to implement this: https://stackoverflow.com/questions/36432028/how-to-convert-a-file-into-byte-array-directly-without-its-pathwithout-saving-f – pschlz Oct 09 '18 at 09:48
  • 1
    Show the code that leads to this exception. Nowhere in that question is anyone using `ReadTimeout`. See also https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1. – CodeCaster Oct 09 '18 at 09:55
  • The official documentation link posted in @CodeCaster comment above has very good code samples to get you started. – cleftheris Oct 09 '18 at 10:21

2 Answers2

0

You can read about file upload here to make sure that you did not missed any steps and in case you need more knowledge about file upload.

Janmejay Kumar
  • 309
  • 2
  • 7
0

In case someone faces similar behavior. Here's my solution for uploading an image and converting it to a byte array (for storing it in a Microsoft SQL database):

First of all use the xaml code and the IFormFile property from the question. In the code behind add this code to process the form data to a byte array:

public async Task<IActionResult> OnPostAsync(string id)
{
    //get the "Kontakt" entity
    if (!await SetKontaktAsync(id))
    {
          return NotFound();
    }

    //convert form data to byte array and assign it to the entity
    if (BildUpload.Length > 0)
    {
          using (var ms = new MemoryStream())
          {
                BildUpload.CopyTo(ms);
                Kontakt.Bild = ms.ToArray();
          }
    }

    //save changes to the database
    _context.Attach(Kontakt).State = EntityState.Modified;
    await _context.SaveChangesAsync();

    //reload page
    return await OnGetAsync(Kontakt.GID);
}

Frameworks: ASP.NET Core 2.2, Entity Framework Core 2.2

pschlz
  • 173
  • 1
  • 12