0

Iam new in asp.net, ajax and c#. Iám trying to save image on server by the following example:

        <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
<body>
<form id="form1" runat="server">
        <input type="file" name="postedFile" />
        <input type="button" id="btnUpload" value="Upload" />
        <progress id="fileProgress" style="display: none"></progress>
        <hr />
        <span id="lblMessage" style="color: Green"></span>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
        $("body").on("click", "#btnUpload", function () {   
$.ajax({
                url: 'Handler.ashx',
                type: 'POST',
                data: new FormData($('form')[0]),
                cache: false,
                contentType: false,
                processData: false,
                success: function (file) {
                    $("#fileProgress").hide();
                    $("#lblMessage").html("<b>" + file.name + "</b> has been uploaded.");
                },
                xhr: function () {
                    var fileXhr = $.ajaxSettings.xhr();
                    if (fileXhr.upload) {
                        $("progress").show();
                        fileXhr.upload.addEventListener("progress", function (e) {
                            if (e.lengthComputable) {
                                $("#fileProgress").attr({
                                    value: e.loaded,
                                    max: e.total
                                });
                            }
                        }, false);
                    }
                    return fileXhr;
                }
            });
        });
        </script>
    </form>
</body>
</html>

Added Generic Handler:

    using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;

namespace RateEatPresentation
{
    /// <summary>
    /// Summary description for Handler
    /// </summary>
    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //Check if Request is to Upload the File.
            if (context.Request.Files.Count > 0)
            {
                //Fetch the Uploaded File.
                HttpPostedFile postedFile = context.Request.Files[0];

                //Set the Folder Path.
                string folderPath = context.Server.MapPath("~/UsersUploadedImages/");

                //Set the File Name.
                string fileName = Path.GetFileName(postedFile.FileName);

                //Save the File in Folder.
                postedFile.SaveAs(folderPath + fileName);

                //Send File details in a JSON Response.
                string json = new JavaScriptSerializer().Serialize(
                    new
                    {
                        name = fileName
                    });
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/json";
                context.Response.Write(json);
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

But Iam getting exceprion "An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code". I tried to find something similar but unfortunately didn't succeeded: enter image description here

3
  • ... additional information: Maximum request length exceeded - looks like you already have the info you need Commented Jan 28, 2020 at 17:40
  • 1 try with a small file (text file with 1 character) 2 if that works search SO for how to increase maximum request length on ASP.Net requests Commented Jan 28, 2020 at 17:41
  • Does this answer your question? Maximum request length exceeded. Commented Jan 28, 2020 at 18:06

1 Answer 1

1

It's possible the image may be too big size, you need ajust the web.config to sopport them. By default IIS supports 4MB you can change that in Web.config

<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>

And

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

You must change the maxRequestLength and maxAllowedContentLength

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

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.