2

以下の私のコードを見つけてください。ユーザーが 4 MB 未満のファイルをアップロードするように制限しようとしていますが、830 KB のファイルを選択すると、コンテンツの長さが 80 MB になります。
このコードflSignature.PostedFile.ContentLengthは機能していません。助けてください。

ティア

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));
            }
        }


        //}
    }
}
4

2 に答える 2

5

ContentLengthプロパティによって保持される値は、キロバイトではなくバイトで表されます。

したがって、 を発行flSignature.PostedFile.ContentLength > 4096すると、実際には、アップロードされたファイルのサイズが 4 メガバイトではなく 4 キロバイトを超えているかどうかを確認しています。

次のようなものを試してください:

if (flSignature.PostedFile.ContentLength > 4096 * 1024)  // 4194304 bytes
{ 
    uploadMsg = "Collective file size is more than 4 MB.";
    break;
}
于 2012-11-01T11:43:19.740 に答える
2

PostedFile.ContentLengthmaxrequestファイルの最大長が必要とするよりも大きなサイズのファイルを参照している場合、機能する必要がありweb.configます

于 2012-11-01T11:26:03.083 に答える