Pdf could been seen as a file.
You could use byte[] to transfer file.
Below is my simple sample.
My contract.
[ServiceContract()]
public interface IFileUpload
{
[OperationContract]
void Upload(byte[] bys);
}
My service. AspNetCompatibilityRequirementsMode.Allowed is used to enable HttpContext or it will be null. Here I directly save the file in the server, if you want to save it in sqlserver , just use varbinary field to save the byte[] of pdf file.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FileUploadService : IFileUpload
{
public void Upload(byte[] bys)
{
string filename = Guid.NewGuid().ToString()+".pdf";
File.WriteAllBytes(HttpContext.Current.Request.MapPath("/upload/") + filename, bys);
}
}
My web.config of wcf service.
The bindingconfiguration ECMSBindingConfig is used to enable uploading large data or the service doesn't allow too large data.
serviceHostingEnvironment's aspNetCompatibilityEnabled should also be set to true or HttpContext will be null.
<service name="Service.CalculatorService" >
<endpoint binding="basicHttpBinding" bindingConfiguration="ECMSBindingConfig" contract="ServiceInterface.ICalculatorService"></endpoint>
</service>
<bindings>
<basicHttpBinding>
<binding name="ECMSBindingConfig" allowCookies="false" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647" bypassProxyOnLocal="true" >
<readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" maxDepth="2147483647"
maxBytesPerRead="2147483647" />
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
/>
My client. User webform as a sample.
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="upload" OnClick="Button1_Click" />
</form>
Code behind. Here I use channelFacotory ,it is similar to client generated by visual studio
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile file = FileUpload1.PostedFile;
using (ChannelFactory<IFileUpload> uploadPdf = new ChannelFactory<IFileUpload>("upload"))
{
IFileUpload fileUpload = uploadPdf.CreateChannel();
byte[] bys = new byte[file.InputStream.Length];
file.InputStream.Read(bys, 0, bys.Length);
fileUpload.Upload(bys);
}
}
Web.config of client.
<client>
<endpoint name ="upload" address="http://localhost:62193/uploadPdf.svc" binding="basicHttpBinding" contract="ServiceInterface.LargeData.IFileUpload" />
</client>
I assume the user uploads pdf, if you want to upload other files , you could add file extension as the service's parameter. Other operation should be similar.