Click here to Skip to main content
15,849,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a form that has some textboxes and a file upload. I am trying to send this data to webmethod using .ajax() method. I have not even been able to hit the breakpoint so far.
JavaScript
var dataToSend = new FormData();           
 dataToSend.append('file',  document.getElementById("myFile").value);
 dataToSend.append('text',  document.getElementById("biddername").value);

            $.ajax({
                type: "POST",
                url: "SupplierMaster.aspx/RegisterSupplier",
                data: dataToSend,
                processData: false,
                contentType: false,
                dataType: false,
                async: true,
                success: function (data, status) {
                    console.log("CallWM");
                    alert(data.d);
                },
                failure: function (data) {
                    alert(data.d);
                },
                error: function (data) {
                    alert(data.d);
                }
            });

        }



C#
[WebMethod]
    public static string RegisterSupplier(HttpPostedFile file, string biddername)
    {

        return "a";
    }
Posted
Comments
Sinisa Hajnal 14-May-15 6:38am    
Check ajax method documentation. Having dataType false, contentType false can only cause grief. IF you don't want to explicitly set those field, don't include them, but don't set them to invalid values.

1 solution

You can't upload files that way as javascript can't access the filesystem to serialise the data. Google "html5 asynch file upload" for a solution for those clients that use html5, and for non-html5 browsers you'll need to use an asynch upload plug in. Google "asp.net asynch upload" and you'll find lots of examples.
 
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