Configure ASP.NET MVC To Accept Large File Uploading

///
///In Web.config
///

<configuration>
    <appSettings>
       ...
    </appSettings>
  

    <system.web>
        <compilation debug="true" targetFramework="4.5"/>
        <httpRuntime targetFramework="4.5"
                     executionTimeout="1000"
                     maxRequestLength="214748364"
                     useFullyQualifiedRedirectUrl="false"
                     minFreeThreads="8"
                     minLocalRequestFreeThreads="4"
                     appRequestQueueLimit="100" />
    </system.web>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <remove name="OPTIONSVerbHandler" />
            <remove name="TRACEVerbHandler" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>

        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="1073741824" />
            </requestFiltering>
        </security>
    </system.webServer>

    <connectionStrings>
        ....
    </connectionStrings> 
</configuration>
///
/// In Controller
///

[ValidateInput(false)]
[HttpPost]
public JsonResult UploadAudio()
{
    try
    {
        foreach (string f in Request.Files.AllKeys)
        {
            var file = Request.Files[f];

            var path = HttpContext.Server.MapPath("~/Voices");
            if (!System.IO.File.Exists(System.IO.Path.Combine(path, file.FileName)))
            {
                file.SaveAs(System.IO.Path.Combine(path, file.FileName));
            }
        }

        return Json("success");
    }
    catch (Exception Ex)
    {
        return Json("Server says:" + Ex.Message, JsonRequestBehavior.AllowGet);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s