上传前验证文件大小

5

我需要验证要上传到服务器的文件。必须在上传之前进行验证,即在客户端完成验证。这个任务应该在ASP.NET MVC3网页中完成。它也应该适用于所有浏览器。IE9、8、7/FF/Chrome。我知道IE没有FileReader API。

我的问题是,在MVC3网页中如何在上传之前验证文件大小。

3个回答

2
您可以使用jQuery实现:
<span>
<b>Attachment</b> (8 MB only)<label id="attached" style="color:red; margin-left:5px"></label>
</span>
<input type="file" id="Attachment" name="Attachment" class="admin_textfeildmedium" value="" style="width:551px" accept="image/*">

jQuery(document).ready(function () {


jQuery('#Attachment').bind('change', function () {
                            //fileUpload = 0;
                            var iSize = (this.files[0].size / 1024);
                            if (iSize / 1024 > 1) {
                                if (((iSize / 1024) / 1024) > 1) {
                                    fileUpload = 0;
                                } else {
                                    iSize = (Math.round((iSize / 1024) * 100) / 100);
                                    if (iSize <= 8) {
                                        fileUpload = 1;
                                    } else {
                                        fileUpload = 0;
                                    }
                                }
                            } else {
                                fileUpload = 1;
                            }
                            if (fileUpload == 0) {
                               // alert("Your attachment exceeded 8MB.");
                                jQuery('#attached').html('Your attachment exceeded 8MB.');
                                jQuery('#Attachment').val('');
                            }

                        });

                    });

1

.Net MVC解决方案:

我正在使用HttpPostedFileBase数据类型

在您的Views > Shared文件夹中,创建一个名为"EditorTemplates"的新文件夹,并使用以下内容:

@model HttpPostedFileBase

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

我然后将这个HttpPostedFileBase对象从控制器传递给一个执行以下操作的方法:

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

    .....
 }

HttpPostedFileBase类的ContentLength属性包含所上传文件的字节数。

这将使您有一个可用的文件上传框。

在ASP.NET WebForms解决方案中:

<asp:FileUpload ID="fuPictures" runat="server" />

创建一个带有OnClick或OnCommand事件的按钮,执行类似以下操作:

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

这将给你文件大小。希望这有所帮助。


@Praveen 这对你有帮助吗? - Termato

0

当涉及到支持HTML 5的浏览器时,可以通过简单的JavaScript轻松实现:

HTML语法

<input type="file" id="myFile" />

Javascript 语法

//gets the element by its id
var myFile = document.getElementById('myFile');

//binds to onchange event of the input field
myFile.addEventListener('change', function() {
  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);

});

然而,当涉及到老旧的浏览器(我们都指向你,Internet Explorer)时,在客户端实现唯一的方法是使用ActiveX:

var myFile = document.getElementById('myFile');

var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = myfile.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
    alert(size + " bytes");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接