2

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll when I try to upload a file to server.

$('#But_Upload').click(function () {
            console.log("its working\n");
            var ajaxRequest = $.ajax({
                type: "POST",
                url: "../HANDLERS/Handler1.ashx",
                contentType: false,
                processData: false,
                data: $('#upload').get(0).files.toString(),
                async: true,
                success: function (xhr) {
                    console.log("done");
                }
            });
        });
    });
</script>

My server code in C# handler is

public void ProcessRequest(HttpContext context)
    {
        System.Diagnostics.Debug.Write(context.ToString());
        try
        {
                HttpPostedFile httpPostedFile = context.Request.Files.Get(0);
                var fileSavePath = HttpContext.Current.Server.MapPath("~/upl") + httpPostedFile.FileName;
                httpPostedFile.SaveAs(fileSavePath);
           }
        catch(Exception E)
        {  }
     }

I am getting the exception exactly at httpPostedFile line,I have been fighting with this for donkey age ,pls help.

4
  • Can you try this answer - stackoverflow.com/questions/11880952/… Commented Mar 27, 2015 at 12:25
  • 1
    @ShrihariIyer - never say what exception/error message you get - we love to guess. Commented Mar 27, 2015 at 13:08
  • @onsjjss I am trying to upload an image .Got any other links? Commented Mar 28, 2015 at 14:14
  • Added solution. Try it now. Commented Mar 31, 2015 at 9:38

2 Answers 2

2

Try this code. There is an special change here for IE and so It works in all browsers. I guess you are facing the problem in IE only?

HttpPostedFile hpf = TryCast(Context.Request.Files(0), HttpPostedFile);

if ((HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")) {
    string[] files = hpf.FileName.Split(new char[] {"\\", c});
    MySerializedobject.FileName = files[(files.Length - 1)];
}
else {
    MySerializedobject.FileName = hpf.FileName;
}

string FileType = hpf.ContentType;
string FileExtension = Path.GetExtension(hpf.FileName);
MySerializedobject.FileType = ("." + MySerializedobject.FileName.Split(".")[1]);
MySerializedobject.FileSize = hpf.ContentLength;
MySerializedobject.FileStream = hpf.InputStream;
Sign up to request clarification or add additional context in comments.

Comments

0

Use This

public void ProcessRequest(HttpContext context)
    {
        HttpPostedFile postedFile = context.Request.Files[0];
        string savepath = "", tempPath = "";
        context.Response.ContentType = "text/plain";
        tempPath = System.Configuration.ConfigurationManager.AppSettings["YourFilePath"];
        savepath = context.Server.MapPath(tempPath);
        string extension = System.IO.Path.GetExtension(postedFile.FileName);
        string filename = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName) + extension;
        if (!Directory.Exists(savepath))
        {
            Directory.CreateDirectory(savepath);
        }
        System.IO.File.Delete(savepath + @"\" + filename);
        postedFile.SaveAs(savepath + @"\" + filename);
    }

on script:

$('#But_Upload').click(function () {
    var files = $("#<%=upload.ClientID %>").get(0).files;
    var test = new FormData();
    test.append(files[0].name, files[0]);
    console.log("its working\n");
    var ajaxRequest = $.ajax({
        type: "POST",
        url: "../HANDLERS/Handler1.ashx",
        contentType: false,
        processData: false,
        data: test,
        async: true,
        success: function (xhr) {
            console.log("done");
        }
    });
});

1 Comment

Getting an exception System.ArgumentOutOfRangeException {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"} pls help

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.