Uploading a file to WCF 3.5 service
I have a .NET 3.5 WCF service. It looks like this:
namespace FileUploaderWcfRestService
{
[ServiceContract]
interface IUploaderService
{
[OperationContract(IsOneWay = true)]
[WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
void UploadFile(string fileName, Stream fileContents);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UploaderService : IUploaderService
{
public void UploadFile(string fileName, Stream fileContents)
{
// save to disk code
}
}
}
In the web.config, I have the endpoint setup as follows:
<endpoint address=""
binding="webHttpBinding"
contract="FileUploaderWcfRestService.IUploaderService">
On the client, I try to call it with a WebClient object, but invariably get error 415 (Unsupported Media Type).
var wc = new WebClient();
string url = "http://localhost:23619/UploaderService.svc/UploadFile?fileName=todo.sdf";
byte[] data = G开发者_StackOverflowetBytesFromFile("ToDo.sdf"); // gets the file into a byte array
byte[] resp = wc.UploadData(url, "POST", data);
I've tried different variations of the URL but nothing helped. What am I missing here?
Have you tried setting the ContentType header on the WebClient to the content type of the file?
wc.Headers.Add("Content-Type","application/octet-stream");
精彩评论