Babak Fakhriloo
Babak Fakhriloo

Reputation: 2126

asp.net mvc fileupload ReadTimeout in HttpPostedFileBase inputstream

I have action in asp.net mvc that accepts a uploaded file. The HttpPostedFileBase I get is not null and even ContentLength has value greater than 0, but when I inspect "InputSream", it has following error :

ReadTimeout = '((System.Web.HttpPostedFileWrapper)refile).InputStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

So when I want to convert to readitbytes as follow, the there is nothing except empty array :

using (var reader = new System.IO.BinaryReader(refile.InputStream))
                    {
                        var a = reader.ReadBytes(model.File.ContentLength);
                    }

So "a" get "{byte[0]}".

What is the problem ?

Upvotes: 1

Views: 3310

Answers (2)

Duy Minh
Duy Minh

Reputation: 11

Abp is trying to validate MVC Action parameters and when it tries to read the value of your files parameter to validate it, this exception occurs.

You can ignore HttpPostedFileWrapper types for validation and everything should work perfectly.

Use this to get byte array from it (more details in : Convert HttpPostedFileBase to byte[])

using (var ms = new MemoryStream())
  {
     refile.InputStream.CopyTo(ms);
     byte[] a = ms.ToArray();

  }

Upvotes: 1

Abhishek K. Upadhyay
Abhishek K. Upadhyay

Reputation: 1032

You might be missing enctype in you form. Please correct your form if there is such kind of issue. A MVC Form with files will be like this

@using (Html.BeginForm("Add", "Advertisement", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
    //Content
}

Thanks Hope this help!

Upvotes: 2

Related Questions