0
    public JsonResult gallaryAudioAdd(Gallery_tbl imageGalleryParam,ImageBal imageParam)
    {
        try
        {
            var Audio_folder_path = Server.MapPath("~/audioClip/");
            TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
            DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);

            var max_id = dc.Gallery_tbls.Count() == 0 ? 0 : dc.Gallery_tbls.Max(x => x.auto_id);



            Gallery_tbl imageGallery_list = new Gallery_tbl();

            imageGallery_list.name = imageGalleryParam.name;
            imageGallery_list.remark = imageGalleryParam.remark;
            imageGallery_list.upload_date = indianTime;
            imageGallery_list.auto_id = int.Parse(max_id.ToString()) + 
            if (imageGalleryParam.gallery_type == "audio")
            {
                string audioClip_name = byteArrayToImage(imageParam.imageData.Split(',')[1], Audio_folder_path, (int.Parse(max_id.ToString()) + 1).ToString() + ".mp3");
                imageGallery_list.gallery_url = "/audioClip/" + audioClip_name;
            }
            imageGallery_list.status = 1;
            imageGallery_list.description = imageGalleryParam.description;
            imageGallery_list.gallery_type = imageGalleryParam.gallery_type;

            dc.Gallery_tbls.InsertOnSubmit(imageGallery_list);
            dc.SubmitChanges();


            return Json(new
            {
                flag = true,
                message = "Insert Successfully",
                data = imageGallery_list
            });
        }
        catch (Exception ex)
        {
            return Json(new
            {
                flag = true,
                message = ex.Message.ToString()
            });
        }
    }


    public static string byteArrayToImage(string Content, string filePath, string filaName)
    {
        var bytes = Convert.FromBase64String(Content);
        using (var imageFile = new FileStream(filePath + filaName, FileMode.Create))
        {
            imageFile.Write(bytes, 0, bytes.Length);
            imageFile.Flush();
        }
        return filaName;
    }

Catching “Maximum request length exceeded”

The problem is that the exception is thrown before the upload button's click-event, and the exception happens before my code is run. So how do I catch and handle the exception?

  • 1
    Possible duplicate of [Maximum request length exceeded](http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded) – bit Dec 24 '15 at 05:13

1 Answers1

0

You cannot catch that exception in your application code, as you have already determined yourself. If the request length is exceeded, the request is rejected already by IIS, before any application code is run.

It is meant as a protection from DoS attacks caused by large requests. You should set a reasonable upper limit (in KBs) based on the maximum size of valid requests that you are expecting:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="8192"/>
  </system.web>
</configuration>
Damir Arh
  • 17,637
  • 2
  • 45
  • 83