Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Ankur\m/ 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

Hello,

Add following line before $.ajax
C#
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,
 
Share this answer
 
Comments
MindFresher 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 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)



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