locked
Validation of file upload from byte array in C#/MVC RRS feed

  • Question

  • User-1188570427 posted

    Hello,

    I need to validate that a byte array length is 1 gb or less.

    Is this the best way for it to occur? Here I am converting it to MegaBytes and checking that?

            /// <summary>
            /// Determines whether [is file size valid] [the specified byte array length].
            /// </summary>
            /// <param name="byteArrayLength">Length of the byte array.</param>
            /// <returns>
            ///   <c>true</c> if [is file size valid] [the specified byte array length]; otherwise, <c>false</c>.
            /// </returns>
            private static bool IsFileSizeValid(int byteArrayLength)
            {
                if (((byteArrayLength / 1024f) / 1024f) > 1024)
                {
                    return false;
                }
    
                return true;
            }

    Wednesday, June 24, 2020 3:02 AM

Answers

  • User-821857111 posted

    If it's an upload, the best thing to do in my opinion is to validate it on the client before your user has to wait for the upload and your server processes it: https://stackoverflow.com/questions/4112575/client-checking-file-size-using-html5

    document.getElementById('fileInput').onchange = function(){
        var filesize = document.getElementById('fileInput').files[0].size;
        if(filesize > 1024*1024*1024){
            alert("too big!");
        }   
    }

    But otherwise, your algorithm is correct.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 5:06 AM
  • User1686398519 posted

    Hi tvb2727,

    I made an example according to your needs, please refer to it.

    • For testing, I reduced the file limit.You can modify it according to your actual needs.
    • It is better to use js to judge directly on the client.

    Controller

            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Upload()
            {
                HttpPostedFileBase file = Request.Files["FileUpload"];
               
                if (!IsFileSizeValid(file.InputStream.Length))
                {
                    TempData["Message"] = "file is too large";
                }
                else
                {
                    //upload
                    TempData["Message"] = "upload success";
                }
                return RedirectToAction("Index");
            }
            private static bool IsFileSizeValid(long byteArrayLength)
            {
                if (byteArrayLength>1024)
                {
                    return false;
                }
                return true;
            }

    Page

    @using (Html.BeginForm("Upload", "Test2", FormMethod.Post, new { enctype = "multipart/form-data"}))
    {
        <input type="file" id="FileUpload" name="FileUpload" />
        <input type="submit" id="UploadBtn" class="btn btn-danger" value="Upload" />
    }
    @TempData["Message"]

    Here is the result.

     

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2020 10:44 AM

All replies

  • User-821857111 posted

    If it's an upload, the best thing to do in my opinion is to validate it on the client before your user has to wait for the upload and your server processes it: https://stackoverflow.com/questions/4112575/client-checking-file-size-using-html5

    document.getElementById('fileInput').onchange = function(){
        var filesize = document.getElementById('fileInput').files[0].size;
        if(filesize > 1024*1024*1024){
            alert("too big!");
        }   
    }

    But otherwise, your algorithm is correct.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 5:06 AM
  • User1686398519 posted

    Hi tvb2727,

    I made an example according to your needs, please refer to it.

    • For testing, I reduced the file limit.You can modify it according to your actual needs.
    • It is better to use js to judge directly on the client.

    Controller

            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Upload()
            {
                HttpPostedFileBase file = Request.Files["FileUpload"];
               
                if (!IsFileSizeValid(file.InputStream.Length))
                {
                    TempData["Message"] = "file is too large";
                }
                else
                {
                    //upload
                    TempData["Message"] = "upload success";
                }
                return RedirectToAction("Index");
            }
            private static bool IsFileSizeValid(long byteArrayLength)
            {
                if (byteArrayLength>1024)
                {
                    return false;
                }
                return true;
            }

    Page

    @using (Html.BeginForm("Upload", "Test2", FormMethod.Post, new { enctype = "multipart/form-data"}))
    {
        <input type="file" id="FileUpload" name="FileUpload" />
        <input type="submit" id="UploadBtn" class="btn btn-danger" value="Upload" />
    }
    @TempData["Message"]

    Here is the result.

     

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2020 10:44 AM