Click here to Skip to main content
14,450,508 members
Rate this:
Please Sign up or sign in to vote.
hi,
I am trying to upload doc and pdf files using webservice.
I have an error like this

System.ArgumentOutOfRangeException was unhandled by user code
  HResult=-2146233086
  Message=Index was out of range. Must be non-negative and less than the size of the collection.


my code is

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string uploadfile(string res)
        {
            HttpContext postedContext = HttpContext.Current;
            HttpPostedFile file = postedContext.Request.Files[0];
            //HttpPostedFile file = HttpContext.Current.Request.Files[formData];
            //HttpPostedFile hFile = postedContext.Request.Files[1];
            string name = file.FileName;

            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName);
            string fileExtension = Path.GetExtension(file.FileName);
            //file.PostedFile.SaveAs("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);
            //File.Copy("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);
            file.SaveAs("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);

            return res;
        }
        static String ISO_Date()
        {
            return DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
        }

jquery function
<script type="text/javascript">
    function finalupload() {
        var res = document.getElementById('txtSocialPassword').value;

        var newfile = document.getElementById("<%=file.ClientID%>").files[0];

        alert(formData);
        //alert(res);
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "HuntableWebService.asmx/uploadfile",
            data: "{'res':'" + res + "'}",
            dataType: "JSON",
            async: true,
            success: function (response) {
                //var res = response.d;
                alert(response.d);
            }
        });
        return false;
    }
</script>


I am trying to save resume in to harddisk folder.
please help me.
Thank you....
Posted
Updated 18-Dec-19 21:12pm
Comments
Kumarbs 22-Jul-14 8:44am
   
Can you please check that res value you are sending to the service is available properly. In the service it is not able to find a file, hence it shows the error outofrange index in " postedContext.Request.Files[0];" line.
NagaRaju Pesarlanka 22-Jul-14 8:46am
   
no issues to send a file.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

put your
var res
in

Sys.Serialization.JavaScriptSerializer.serialize();


after that try your code...

$.ajax({
            type: "POST",
            contentType: "application/json",
            url: "HuntableWebService.asmx/uploadfile",
            data: "{'res':'" + Sys.Serialization.JavaScriptSerializer.serialize(res) + "'}",
            dataType: "JSON",
            async: true,
            success: function (response) {
                //var res = response.d;
                alert(response.d);
            }
        });
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100