Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET WCF jQuery
Here is what I am trying to do (unsuccessfully, I might add) and would appreciate any direction you can give me
 
From my HTML5 site, I want to upload a file to a cross domain WCF service that is hosted in IIS 7.5.
 
In addition to uploading the files, I need to send additional parameters to the upload fucntion on the server
 
Is this possible to do?
 
Here is what my operationContract looks like:
 

[OperationContract]
    [WebInvoke( Method = "POST",
    UriTemplate = "/uploadmodeldata/?id={Id}&customerdatatype={customerdatatype}&data={data}")]
    void UploadModelData(string Id, string customerdataType, byte[] data);
 
Here is what my jquery ajax request
 
 function FileVisits() {
   
        var uid = checkCookie1();
        userid = uid.toString().replace(/"/g, '');
        var fileData = JSON.stringify({
       Id:userid ,customerdatatype:scanupload,
            data: $('#fileBinary').val()
        });
        alert(fileData);
            "use strict";
            var wcfServiceUrl = "http://xxxxx:1337/Service1.svc/XMLService/";
            $.ajax({
                cache: false,
                url: wcfServiceUrl + "uploadmodeldata/",               
                data: fileData,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "json",
                headers:    {
                            'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey',
                            'Accept': 'application/atom+xml,application/xml,text/xml',
                        },
                beforeSend: function (xhr) {
                    $.mobile.showPageLoadingMsg();
                  
                    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                   
                  
                },
                complete: function () {
                    $.mobile.hidePageLoadingMsg();
                },
    
                success: function (data) {
                    var result = data;
                  
    
                },
                error: function (data) {
                    alert("Error");
                }
            });
        
    }
 
if file size is less then 100 kb this error occurred
 
> Method not allowed
 
but if file is greater then 100 kb this error occurred
 
> 413 Request entity to large
 
How can I upload a file from jquery ajax to cross domain wcf.
Thanks
Posted 19-Mar-13 3:01am
Comments
Ankur\m/ at 19-Mar-13 8:39am
   
These might also help - https://www.google.co.in/search?q=Method+not+allowed&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=rcs

1 solution

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

Solution 1

Hello,
 
Add following line before $.ajax
jQuery.support.cors = true;
Then add setting parameter crossDomain: true, to your $.ajax call and finally change dataType to text. It should allow you to POST.
 
Please also have a look at this[^] CodeProject article as well to know more about ajax uploading without JQuery.
 
Regards,
  Permalink  
Comments
MindFresher at 19-Mar-13 9:12am
   
thanks for reply. i have set jQuery.support.cors = true; and crossDomain: true, dataType to text. But its not working. 400 Bad request error occurred.
Prasad Khandekar at 19-Mar-13 10:21am
   
Hello,
 
If you have firefox and HTTPFox plugin try to look at the request and see if it's well formed. The 400 Bad Request means that the data stream sent by the client (e.g. your Web browser or our CheckUpDown robot) was 'malformed'. Go through this (http://www.codeproject.com/Articles/169928/WCF-Service-returns-400-Bad-Request) article to see if anything applies to your service.

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

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 186
4 Aajmot Sk 153
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 19 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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