3

I'm developing a module to send emails, and one of the features is to upload multiple files...

I used this as a reference to create it: http://www.dotnetcurry.com/ShowArticle.aspx?ID=68

I also had to add jquery to allow it to upload multiple files.

<script src="Scripts/jquery-1.3.2.js" type="text/javascript">
</script>

<script src="Scripts/jquery.MultiFile.js" type="text/javascript">
</script>

However, if I upload a file that is larger than 10MB when I press a button in my form (I guess it happens whenever a postback occurs), the browser cannot load the page. So what I want is to trigger this function in my button:

protected void imgBtnEnviar_Click(object sender, ImageClickEventArgs e)
{
    int intBytes = 0;
    string strArchivos = "";
    string strRutaArchivos = "";
    bool blnBytes = true;
    bool blnPermitir = true;

    try
    {
        HttpFileCollection hfc = Request.Files;

        //Check if all files together are less than 10mb
        foreach (HttpPostedFile hpf in hfc)
        {
            intBytes += hpf.ContentLength;

            if (intBytes > 11000000)
            {
                blnBytes = false;
                break;
            }
        }

        if (blnBytes)
        {
            //Upload All attached files to server
            foreach (HttpPostedFile hpf in hfc)
            {
                if (hpf.FileName == "")
                {
                    break;
                }

                string strNombre = ((int)Session["Grupo"]) + "_" + (int)Session["EmailIdCliente"] + "_" + DateTime.Now.ToString("MM-dd-yyyy_HH.mm.ss") + "_" + Path.GetFileName(hpf.FileName);
                strArchivos += strNombre + ";";
                strRutaArchivos += Server.MapPath("~/Archivos/") + strNombre + ";";
                hpf.SaveAs(Server.MapPath("~/Archivos/") + strNombre);
            }
        }
    }

    catch
    {
        blnPermitir = false;
    }

    if (!blnBytes || !blnPermitir)
    {
        //Error Message Displayed To User
        ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "alert('Maximo Puede Enviar 10MB En Archivos');", true);
    }

    else
    {
        //Moving on to send email
    }
}

What I want is that if blnBytes equals false (it becomes false when all combined files are more than 10mb) it just displays an error message, but how can I make it work even if a single file is larger than 10mb??....

Hope you can help me with that thanks

2 Answers 2

2

You have to enable large file support in web.config (httpRuntime, maxRequestLength parameter).

Here is sample:

    <httpRuntime requestValidationMode="2.0"
     executionTimeout="6000"
     maxRequestLength="20485760"
     requestLengthDiskThreshold="80"
     useFullyQualifiedRedirectUrl="false"
     minFreeThreads="8"
     minLocalRequestFreeThreads="4"
     appRequestQueueLimit="5000"
     enableKernelOutputCache="true"
     enableVersionHeader="true"
     requireRootedSaveAsPath="true"
     enable="true"
     shutdownTimeout="90"
     delayNotificationTimeout="5"
     waitChangeNotification="0"
     maxWaitChangeNotification="0"
     enableHeaderChecking="true"
     sendCacheControlHeader="true"
     apartmentThreading="false" />

More about this can be found here: http://msdn.microsoft.com/en-us/library/e1f13641(v=vs.71).aspx

Sign up to request clarification or add additional context in comments.

Comments

2

You may try this..

foreach (HttpPostedFile hpf in hfc)
{
    if(hpf.ContentLength > 11000000)
    {
        blnBytes = false;
        break;
    }
    intBytes += hpf.ContentLength;
    if (intBytes > 11000000)
    {
        blnBytes = false;
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.