【问题标题】:restrict files upload with specific size限制特定大小的文件上传
【发布时间】:2015-08-23 07:29:27
【问题描述】:

我有 vb.net 网络应用程序,我在互联网上搜索限制用户上传超过 10MB 文件的代码,但是,在我在我的 Visual Studio 中调试应用程序时使用了代码后工作。

现在,当我将调试的文件移动到服务器时,它不起作用!!!更困扰我的是代码在谷歌浏览器中运行良好但在 Internet Explorer 中运行良好的方式!!!

Internet Explorer 11.0.22 版、谷歌浏览器 44.0 版

<%@ Page Language="VB" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
    <title></title>    
</head>
<body>
    <form id="Form1" method="post" runat="server">
			<table>
                <tr>
                    <td width="10%" align="left">
                        <asp:Label Style="z-index: 0" ID="Label10" runat="server" Font-Names="Comic Sans MS" Font-Size="X-Small"> ATTACHMENT (IF AVALIABLE)</asp:Label></td>
                    <td width="90%">
                        <input style="z-index: 0" id="MyFile" type="file" name="myFile" runat="server">
                        <asp:Label Style="z-index: 0" ID="Label17" runat="server" Font-Bold="True" Width="197px" ForeColor="Red">Maximum File Size = 10MB</asp:Label></td>
                    <td width="1%"></td>
                </tr>
                <tr>
                    <td width="10%" align="left"></td>
                    <td width="90%">
                        <asp:Button Style="z-index: 0" ID="Button1" runat="server" Font-Names="Comic Sans MS" Height="32px"
                            Width="112px" Text="SUBMIT" BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px"></asp:Button></td>
                    <td width="1%"></td>
                </tr>
            </table>
    </form>
    <input type="hidden" value="0" name="fisize" id="fisize" />
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";                
            $(document).ready(function () {
            $('#<%= Page.FindControl("MyFile").ClientID%>').change(function() {
            //$("#MyFile").change(function () {
           //var fi = document.getElementById("MyFile");
            var fi =document.getElementById('<%=Page.FindControl("MyFile").ClientID%>');
                    var dd = fi.files[0].size;
                    if (dd > 10485760) {
                        alert("the selected file size is more than 10MB, please select another file !!");
                        confirm_value.value = "No";
                    }
                    else {
                        alert("okay :)");
                        confirm_value.value = "Yes";
                    }
                    document.forms[0].appendChild(confirm_value);
                });
            });
        </script>
</body>
</html>

【问题讨论】:

  • 您是否验证了web.config 填充中的maxRequestLength 设置是否正确?它应该超过 10MB 以允许大小为 10MB 的文件。
  • 我刚刚更新为 'maxrequestlength=103000' 但仍然没有任何改变!!

标签: javascript html asp.net vb.net


【解决方案1】:

为什么不使用 ASP.NET 文件上传控件?

然后:

int max = 2097152; // ~2MB = 1024 * 2048
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLeng­th <= max)
{
   //save

}

【讨论】:

  • 这很好,我暂时放弃了 javascript :( ,谢谢大家 :)
猜你喜欢
  • 1970-01-01
  • 2011-01-29
  • 2017-03-09
  • 2011-01-13
  • 1970-01-01
  • 2022-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多