Validate File size before upload

后端 未结 3 1705
野性不改
野性不改 2021-01-06 19:14

I need to validate the file which is to be uploaded to the server. The validation must be done before uploading it. i.e., validation completed at client side. This task shou

3条回答
  •  时光取名叫无心
    2021-01-06 19:39

    .Net MVC Solution:

    I am using the data type of HttpPostedFileBase

    In your Views > Shared Folder, create a new folder called "EditorTemplates" and use this:

    @model HttpPostedFileBase
    
    @Html.TextBox("", null, new { type = "file" })
    

    I then pass this HttpPostedFileBase object from the controller to a method that does the following:

     public Files Upload(HttpPostedFileBase files)
     {
        if (files.ContentLength > 0) {
    
        .....
     }
    

    The ContentLength property on the HttpPostedFileBase class contains the number of bytes in the posted file

    This will make it so you have a file uploading box available.

    On the ASP.NET WebForms Solution:

    
    

    Make a button with a OnClick or OnCommand event that does something like this:

    if (fuPictures.HasFile == true)
    {
        int fileSize = fuPictures.FileBytes;
    }
    

    That will give you the file size. Hope this helps.

提交回复
热议问题