c# - 限制用户上传大文件

标签 c# asp.net

请在下面找到我的代码。我试图限制用户上传小于 4 MB 的文件,但当我选择 830 KB 的文件时,我得到的内容长度为 80 MB。
此代码 flSignature.PostedFile.ContentLength 无效。请帮忙。

TIA

string uploadMsg = "";
string appPath = Server.MapPath("~");
string parentpath = appPath + "\\app\\Pictures\\";
//To Upload Multiple Files on Single Click 
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
    HttpPostedFile hpf = hfc[i];

    if (hpf.ContentLength > 0)
    {
        //if (hpf.ContentLength > 4096)
        //{
        //   uploadMsg = "Collective file size is more than 4 MB.";
        //}
        //else
        //{
        if (hfc.AllKeys[i].Contains("flSignature"))
        {
            if (flSignature.PostedFile.ContentLength > 4096)
            { 
                uploadMsg = "Collective file size is more than 4 MB.";
                break;
            }
            else
            {
                if (Path.GetFileName(hpf.FileName).ToLower().Contains("xls") || Path.GetFileName(hpf.FileName).ToLower().Contains("doc"))
                {
                    showalert("Only Image can be uploaded.");
                }
                else
                {
                    hpf.SaveAs(parentpath + lblUniqueNo.Text + "_signature_" + Path.GetFileName(hpf.FileName));
                }
            }
        }
        else if (hfc.AllKeys[i].Contains("flPhoto"))
        {
            if (flPhoto.PostedFile.ContentLength > 4096)
            {
                uploadMsg = "Collective file size is more than 4 MB.";
                break;
            }
            else
            {
                if (Path.GetFileName(hpf.FileName).ToLower().Contains("xls") || Path.GetFileName(hpf.FileName).ToLower().Contains("doc"))
                {
                    showalert("Only Image can be uploaded.");
                }
                else
                {
                    hpf.SaveAs(parentpath + lblUniqueNo.Text + "_passport_" + Path.GetFileName(hpf.FileName));

                }
            }
        }
        else if (hfc.AllKeys[i].Contains("flIdentDoc"))
        {
            if (flIdentDoc.PostedFile.ContentLength > 4096)
            {
                uploadMsg = "Collective file size is more than 4 MB.";
                break;
            }
            else
            {
                hpf.SaveAs(parentpath + lblUniqueNo.Text + "_doc_" + Path.GetFileName(hpf.FileName));
            }
        }


        //}
    }
}

最佳答案

ContentLength携带的值属性以字节而不是千字节表示。

因此,当您发出 flSignature.PostedFile.ContentLength > 4096 时,您实际上是在检查上传文件的大小是否大于 4 KB,而不是 4 MB。

尝试这样的事情:

if (flSignature.PostedFile.ContentLength > 4096 * 1024)  // 4194304 bytes
{ 
    uploadMsg = "Collective file size is more than 4 MB.";
    break;
}

关于c# - 限制用户上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13175953/

相关文章:

c# - PowerShell 在 NavigateComplete2 事件触发时崩溃

c# - 如何清理 MonoTouch 中的线程?

c# - 表示 Day 的最大值为 32 的日期

javascript - 如何在 C# 中使用 javascript 日历设置值后获取文本框的文本?

c# - 在 asp.net 中处理危险字符的最佳做法是什么?

c# - 在 Controller (ASP.NET Core)中进行 DI 时如何在 SignalR 中使用 Clients.Caller 和 Clients.Others?

C# 数据收集与数据库查找 - 哪个更有效?

c# - 在 VB.NET/C# 中调用泛型函数

c# - 将字节数组转换为 C# 中的枚举集合

c# - 使用 : The magic number in GZip header is not correct. 解压缩上传的文件确保您传入的是 GZip 流