测量在ASP.NET中使用jQuery上传的文件大小

时间:2012-04-04 10:49:07

标签: javascript jquery asp.net .net file-upload

如何使用jquery测量文件大小,以下代码在firefox,chrome中工作正常,但它在IE(7/8/9)中不起作用。任何人都可以帮我如何在IE中测量文件大小

var fi = document.getElementById('loadfile');
var sizeInMB = fi.files[0].size;
    sizeInMB = (sizeInMB / (1024 * 1024));
if (sizeInMB > 20)
    alert("File size more than 20MB");
else
    alert("File size less than 20MB");

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

<script runat="server">

    protected void UploadButton_Click(object sender, EventArgs e)
    {

        string savePath = @"c:\temp\uploads\";


        if (FileUpload1.HasFile)
        {                
            // Get the size in bytes of the file to upload.
            int fileSize = FileUpload1.PostedFile.ContentLength;

            // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
            if (fileSize < 2100000)
            {

                // Append the name of the uploaded file to the path.
                savePath += Server.HtmlEncode(FileUpload1.FileName);


                FileUpload1.SaveAs(savePath);


                UploadStatusLabel.Text = "Your file was uploaded successfully.";
            }
            else
            {

                UploadStatusLabel.Text = "Your file was not uploaded because " + 
                                         "it exceeds the 2 MB size limit.";
            }
        }   
        else
        {

            UploadStatusLabel.Text = "You did not specify a file to upload.";
        }
    }
</script>