Here i will show how to upload a file in WCF serve rice.The streaming concept is used to implement a wcf service to upload a file.I have created a wcf project i:e UploadFile.svc then Open the IUploadFile.cs and add following code
Configuration File:
Now we have to give some configuration settings for wcf service.
<system.servicemodel>
<services>
<service behaviorconfiguration="streamServiceBehaviour" name="UploadFile">
<endpoint address="" binding="basicHttpBinding" bindingconfiguration="streamBindingConfig" contract="IUploadFile">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</service>
</services>
<behaviors>
<servicebehaviors>
<behavior name="streamServiceBehaviour">
<servicedebug includeexceptiondetailinfaults="true">
<servicemetadata httpgetenabled="true">
</behavior>
</servicebehaviors>
</behaviors>
<bindings>
<basichttpbinding>
<binding name="streamBindingConfig" transfermode="Streamed">
</binding>
</basichttpbinding>
</bindings>
</system.servicemodel>
Then i have add a new with file upload control.Then add the following code to code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
[ServiceContract]
public interface IUploadFile
{
[OperationContract]
string FileUpload(Stream inputStream);
[OperationContract]
Stream FileDownload(string fId);
[OperationContract]
string[] GetFiles();
}
CodeBehind:using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Web;
public class UploadFile : IUploadFile
{
private const string PATH = @"C:\wcf\download";
private string GetDirectoryinfoPath()
{
return PATH;
}
public string FileUpload(System.IO.Stream inputStream)
{
string fID = string.Format(@"{0}\{1}.txt", GetDirectoryinfoPath(), Guid.NewGuid().ToString());
StreamReader sreader = new StreamReader(inputStream);
string filecontents = sreader.ReadToEnd();
File.WriteAllText(fID, filecontents);
return fID;
}
public string[] GetFiles()
{
return new DirectoryInfo(GetDirectoryinfoPath()).GetFiles().Select(x => x.FullName).ToArray();
}
}
Configuration File:
Now we have to give some configuration settings for wcf service.
<system.servicemodel>
<services>
<service behaviorconfiguration="streamServiceBehaviour" name="UploadFile">
<endpoint address="" binding="basicHttpBinding" bindingconfiguration="streamBindingConfig" contract="IUploadFile">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</service>
</services>
<behaviors>
<servicebehaviors>
<behavior name="streamServiceBehaviour">
<servicedebug includeexceptiondetailinfaults="true">
<servicemetadata httpgetenabled="true">
</behavior>
</servicebehaviors>
</behaviors>
<bindings>
<basichttpbinding>
<binding name="streamBindingConfig" transfermode="Streamed">
</binding>
</basichttpbinding>
</bindings>
</system.servicemodel>
Then i have add a new with file upload control.Then add the following code to code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class UploadExample : System.Web.UI.Page
{
StreamService.FileStreamClient wclient = new StreamService.FileStreamClient();
string fileId = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
fId=wclient.FileUpload(new MemoryStream(FileUpload1.FileBytes));
}
}
No comments:
Post a Comment