上传到文件系统之前,请检查文件大小 [英] Check filesize before uploading to file system

查看:316
本文介绍了上传到文件系统之前,请检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题,

我已经做了很多的研究已经这样,但我有不同的方法。

I have done a lot of research on this already but I have a different approach.

我的问题:我有一个文件上传,在使用asp.net用VB所有浏览器。问题是,我们的系统只允许文件上传是不超过 1MB 大。现在,使用的背,一查到底,它会告诉用户如果文件过大,他们必须上传较小的文件。然而,奇怪的问题是,它会赶上一个文件,是的 2-3mb 超过限制。没有文件是否例如20MB。如果上传,我会得到一个讨厌的调试错误称最大文件大小已要求。

My problem: I have a file uploader that works in all browsers using asp.net with VB. The issue is that our system only allows files to be uploaded that are no bigger than 1mb. Now, using a check on the back-end, It will tell the user if the file is too big, and that they must upload a smaller file. However, the weird problem is that it will catch a file that is 2-3mb over the limit. Not if a file is 20mb for example. If upload it, I will get a nasty debug error saying the max file size has been requested.

我想要做的是在前端以preven它甚至被发送到后端进行快速检查。

What I wanted to do was a quick check on the front end to preven it from even being sent to the backend.

我用:

$(function(){
    $('#File1').bind('change', function() {
          alert(this.files[0].size);
        });
});

要检查文件的大小。它工作在Chrome和Firefox。但不是IE浏览器。我听说使用ActiveX如果用户允许其在隐私设置可以工作。

To check the file size. It works in chrome and firefox. But not IE. I heard using ActiveX could work if the user allows it in the privacy setting.

我可以使用浏览器检测说嘿,如果IE做的ActiveX做别的了jQuery不过,我想知道是否有比的ActiveX一个更可靠的方法?

I could use browser detection to say "hey, if IE do activeX else do the jQuery" But I am wondering if there is a more reliable method than ActiveX?

推荐答案

您的问题是由于一个事实,即前10的所有的IE版本有HTML5文件API的支持。因此,考虑到这个 HTMLInputElement ,以下将不起作用:

Your problem is due to the fact that all IE versions prior to 10 have no support for the HTML5 File API. So, considering that this is your HTMLInputElement, the following won't work:

this.files[0].size;

原因是, HTMLInputElement.FileList 不因为缺少文件API的支持存在,但你能得到的文件与 HTMLInputElement名。值。但是,这不是你想要的。你想获得该文件的大小。再次,由于缺乏文件API的支持,IE< 10不提供文件的大小的信息。因此,要检查文件的大小为这些小版本的唯一方法,就是使用ActiveX,即使没有人喜欢这样做。

The reason is that HTMLInputElement.FileList does not exist since the lack of File API support, but you could get the file's name with HTMLInputElement.value. But that's not what you want. You want to get the file's size. Once again, because of the lack of the File API support, IE < 10 doesn't supply the file size information. So, the only way to check the file size for these lesser versions, is to use ActiveX, even if nobody likes doing so.

第一个解决方案(客户端 - jQuery的)

您建议:

我可以使用浏览器检测说嘿,如果IE做的ActiveX做别的了jQuery

I could use browser detection to say "hey, if IE do activeX else do the jQuery"

如果你想这样做,你可以有这样的事情:

If you want to do so, you could have something like that:

$(function(){
    $('#File1').bind('change', function() {
        var maxFileSize = 1024000; // 1MB -> 1000 * 1024
        var fileSize;

        // If current browser is IE < 10, use ActiveX
        if (isIE() && isIE() < 10) {
            var filePath = this.value;
            if (filePath != '') {
                var AxFSObj = new ActiveXObject("Scripting.FileSystemObject");
                var AxFSObjFile = AxFSObj.getFile(filePath);
                fileSize = AxFSObjFile.size;
            }
        } else {
            // IE >= 10 or not IE

            if (this.value != '') {
                fileSize = this.files[0].size;
            }
        }

        if (fileSize < maxFileSize) {
            // Enable submit button and remove any error message
            $('#button_fileUpload').prop('disabled', false);
            $('#lbl_uploadMessage').text('');
        } else {
            // Disable submit button and show error message
            $('#button_fileUpload').prop('disabled', true);
            $('#lbl_uploadMessage').text('File too big !');
        }
    });
});

// Check if the browser is Internet Explorer
function isIE() {
    var myNav = navigator.userAgent.toLowerCase();
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
} 

警告!

在盲目照搬这个code,注意解决的ActiveX真是一个可怜的。为了使它正常工作,用户必须改变其 Internet选项的。此外,该解决方案是公共场所没有好,只用于Intranet应用程序。

Before blindly copying this code, note that the ActiveX solution is really a poor one. To make it work, the user must change its Internet Options. Also, this solution is no good for public sites, only for intranet apps.

不过,我想知道是否有比的ActiveX一个更可靠的方法?

But I am wondering if there is a more reliable method than ActiveX?

没有,没有针对IE&LT; 10

No, there is not for IE < 10

第二类解决方案(jQuery插件)

您可以在 jQuery的文件上传。您可以轻松地得到它的大小。

You could the jQuery File Upload. You can easily get the size with it.

解决方案三(服务器端 - VB.NET)

考虑到你有这些ASP控件:

Considering you have these asp controls:

<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="button_fileUpload" runat="server" Text="Upload File" />
<asp:Label ID="lbl_uploadMessage" runat="server" Text="" ForeColor="Red" />

您可以检查选择文件的大小一旦有与服务器端的交互。举例来说,我在这里,一旦用户点击上传按钮检查文件的大小。

You can check the chose file size once there is an interaction with the server side. For instance, here I am checking the file's size once the user clicks on the Upload Button.

Protected Sub btnFileUpload_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button_fileUpload.Click
    ' A temporary folder
    Dim savePath As String = "c:\temp\uploads\"
    If (fileUpload.HasFile) Then
        Dim fileSize As Integer = fileUpload.PostedFile.ContentLength
        ' 1MB -> 1000 * 1024
        If (fileSize < 1024000) Then
            savePath += Server.HtmlEncode(fileUpload.FileName)

            fileUpload.SaveAs(savePath)

            lbl_uploadMessage.Text = "Your file was uploaded successfully."

        Else
            lbl_uploadMessage.Text = "Your file was not uploaded because " +
                                     "it exceeds the 1 MB size limit."
        End If
    Else
        lbl_uploadMessage.Text = "You did not specify a file to upload."
    End If
End Sub

修改

正如你所说,这最后的解决方案不与过大的文件。为了让此解决方案工作,你必须增加你的 web.config中的最大上传文件大小文件:

As you said, this last solution doesn't work with files which are too big. To allow this solution to work, you must increase the max upload file size in your web.config file:

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="52428800" /> <!--50MB-->
    </system.web>
</configuration>

这篇关于上传到文件系统之前,请检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆