Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
XML
<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 20: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

put your
HTML
var res
in

JavaScript
Sys.Serialization.JavaScriptSerializer.serialize();


after that try your code...

JavaScript
$.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);
            }
        });
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900