Upload Files using WCF Rest Service
1. Interface Class
2. Service Implementation Class
Here I added two methods to convert stream into a byte array. You can use one ofmethods in your implementation.
3. Service Configuration
[ServiceContract]
public interface IUpload
{
/// <summary>
/// Upload, runs synchronously service side .
/// </summary >
/// <param name="token">An application arbitrary piece of data. Can be used for request obfuscation.</param >
/// <param name="data">The data being uploaded.</param>
[OperationContract]
[WebInvoke(UriTemplate = "upload/{token}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void Upload(string token, Stream data);
}
2. Service Implementation Class
public class UploadDataService : IUpload
{
public void Upload(string token, Stream data)
{
StreamToFile(data);
}
private void StreamToFile(Stream data)
{
// get full path to and create the directory to save file in
var arrBytes = ConvertStreamToByteArray(data);
File.WriteAllBytes(@"C:\Foo.data", arrBytes);
}
private static byte[] ConvertStreamToByteArray(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
private static byte[] ConvertStreamToByteArray(Stream input)
{
using (var streamReader = new MemoryStream())
{
input.CopyTo(streamReader);
return streamReader.ToArray();
}
}
}
}
Here I added two methods to convert stream into a byte array. You can use one of
3. Service Configuration
<? xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="streamWebHttpBinding" maxReceivedMessageSize="1000000000000" receiveTimeout="01:00:00" sendTimeout="01:00:00" transferMode="Streamed" />
<!--<binding name="streamWebHttpBinding" receiveTimeout="01:00:00" sendTimeout="01:00:00" transferMode="Streamed" />-->
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="LargeUploadBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="RestBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="WcfService1.UploadDataService" behaviorConfiguration="LargeUploadBehavior">
<endpoint behaviorConfiguration="RestBehavior" address="" contract="WcfService1.IUpload" binding="webHttpBinding" bindingConfiguration="streamWebHttpBinding" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Comments
Post a Comment