Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
783 views
in .Net Framework by 31 42 54
I am working on an MVC solutions and I need to Convert HttpPostedFile To Byte Array in .NET , How can I do that in .NET?

1 Answer

0 like 0 dislike
by 31 42 54
 
Best answer

Convert HttpPostedFile To Byte Array in .NET

To Convert HttpPostedFile To Byte Array in .NET, you should follow the below steps in details:

1 - Write a method with name ConvertHttpPostedFileToByteArray that covert file to Byte[]

public byte[] ConvertHttpPostedFileToByteArray(HttpPostedFileBase file)
{
    try
    {
        if (file != null && file.ContentLength > 0)
        {
            using (BinaryReader reader = new BinaryReader(file.InputStream))
            {
                return reader.ReadBytes(file.ContentLength);
            }
        }
    }
    catch (Exception ex)
    {
        LogMiddleware.LogError(ex, "ConvertHttpPostedFileToByteArray");
        return null;
    }
    return null;
}
If you don’t ask, the answer is always NO!
...