Reputation: 11
I would like to upload image from javascript to wcf service using post , datacontract . I read in google , using datacontract it is not possible , it can be done with message contract , because when one parameter is stream , it should not have any other parameter .
However in some links it is mentioned that it can be done by passing parameter in querystring . (ex : WCF Restful service file upload with multi-platform support ) I tried that , still getting error saying saying if stream is there as parameter , it should not contain any other parameter. But it it not possible even with query string? The following is my code
[OperationContract]
[WebInvoke(Method = "POST",UriTemplate = "/UploadFile/userId={userId}", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void UploadFile(string userId,Stream uploadingDetails);
Upvotes: 0
Views: 218
Reputation: 87
The Stream paramenter should be last parameter in your operation contract. If you use v3.5 or 4.0 you will see that error. But you can ignore it. Or else you can try doing like:
[DataContract]
public class UploadData
{
[DataMember]
string userId { get; set; }
[DataMember]
Stream uploadingDetails { get; set; }
}
public class YourService : IYourService
{
public bool UploadFile(UploadData request)
{
}
}
or you have one more option while using Stream, just try to avoid .svc in ur url. add the below code:
public class Global : System.Web.HttpApplication
{
RouteTable.Routes.Add(new ServiceRoute("yourApp/yourService/upload", new WebServiceHostFactory(), typeof(YourService)));
}
and decorate the below attribute to your service:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
and call your service as below:
http://localhost:123456/yourApp/yourService/upload/{userId}
Upvotes: 0