Click here to Skip to main content
15,847,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Folks,

Please tell me how to send the file from JQuery to C#.

Below is my code

JavaScript
$('#btnUpload').click(function () {
            $('#fileUpload').trigger('click');
         });
         $('#btnSave').click(function () {
            $.ajax({
               type: "POST",
               url: "MultipleFileUploadUsingService.aspx/Upload",
               data: $('#fileUpload')[0].files["0"],
               contentType: "image/jpeg; charset=utf-8",               
               success: function () { alert($('#fileUpload')[0].files["0"].name); }
            });
         });    


C#
[System.Web.Services.WebMethod]
        public static void Upload(Byte[] docbinaryarray)
        {
                   
        }


When i send the data with parameter and its contenttype: "application/json", then i receive the value in C# but when i send the file it won't.

Please anybody help me
Thanks & Regards
Kanag.M
Posted

Should not the content type be: 'multipart/form-data'

BTW, why not use already available jQuery plugins to do all the work for you? look at this sample and try:
Tutorials:Multiple File Upload Magic[^]
Ajax File Upload[^]
AJAX Multiple File Upload Form Using jQuery[^]
 
Share this answer
 
you can used like this:-


C#
$.ajax({
                    type: "post",
                    url: "rizwan.aspx/addUserMethod",
                    data: "{Value:'" + $("#hdn").val() + "',groupId:'" + $("#drpList").val() + "',Email_Id:'" + $("#txtemail").val() + "' }",

                    contentType: "application/json; charset=utf-8",
                    dataType: "json",

                    success: function (data) {
                        alert(data.d)
                        $("#DivDetails").hide();
                    },
                    error: function () {
                        alert('Problem');
                    }
                });


on cs file:-
  [WebMethod]
        #region Ajax Methods
        public static string addUserMethod(string Value, string groupId, string Email_Id)
        {
           //do some thing
            return str;
            }

          
        }
        #endregion
 
Share this answer
 
v2
Comments
kanagmathes 6-Jul-12 7:02am    
its fine but how to send jpg file to c#
rizwan muhammed khan gouri 12-Jul-12 1:43am    
$.ajax(
{
type: "POST",
url: "../ajaxURLs/InsertDocument.aspx?requestNumber=" + reqNum + "&fileName=" + fileName,
contentType: 'multipart/form-data',
cache: false,
success: function (html) {
alert("File Inserted!")
},
error(jqXHR, textStatus, errorThrown) {
alert( "Error: " + textStatus + ": " + errorThrown );
}
});
Put these codes in aspx page Call the function DemoAjaxCall() button click
JavaScript
<script type="text/javascript">

    function DemoAjaxCall() {
            $.ajax({
                type: "post",
                data:"{xyz:'"+23+"'}",
                url: "WebForm1.aspx/DemoAjaxCall",
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            });
        }
</script>

In aspx.cs page write these bellow code.
C#
[WebMethod]
       public static int DemoAjaxCall(int abc)
       {
           return 1;
       }
 
Share this answer
 
v2

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