Introduction
Wcf File Upload Limit Password
The default message size is 64K (the maximum size it can support is 64MB), but you should increase the message size if you need to use streamed mode and transfer large chunks of data. This is the third post on a small series about transferring large files with WCF using streaming:File Transfer With WCFFile Transfer With WCF: Part IIFile Transfer With WCF: Part IIII am closing this series of posts by sharing a skeleton for the client side handling of the uploaded and downloaded files. Please note that the following are excerpt from a larger context and so will not compile.
Kendo UI has a nice widget ‘File Upload’, by which we can upload a file(or multiple files) from client machine to server side. In this tip, we will try to upload files using WCF service.
The task is very much simple with MVC application and controller, but whenever we use WCF service and Kendo File Upload, we have to follow the below steps for uploading file(files) to server.
Step – 1
Wcf File Upload Limit Yahoo
The first step is to make our wcf service to accept streamed data, and the setting maximum allowable size.
<system.serviceModel> |
<bindings> |
<webHttpBinding> |
<binding name='WebConfiguration' maxBufferSize='65536' maxReceivedMessageSize='2000000000' transferMode='Streamed'> |
</binding> |
</webHttpBinding> |
</bindings> |
<behaviors> |
<endpointBehaviors> |
<behavior name='WebBehavior'> |
<webHttp /> |
</behavior> |
</endpointBehaviors> |
<serviceBehaviors> |
<behavior name='FileUploadApplication.FileUploadWcfService.KendoFileUploadServiceBehavior'> |
<serviceMetadata httpGetEnabled='true' httpGetUrl=' /> |
<serviceDebug includeExceptionDetailInFaults='false' /> |
</behavior> |
</serviceBehaviors> |
</behaviors> |
<services> |
<service name='FileUploadApplication.FileUploadWcfService.KendoFileUploadService' behaviorConfiguration='FileUploadApplication.FileUploadWcfService.KendoFileUploadServiceBehavior'> |
<endpoint address=' binding='webHttpBinding' behaviorConfiguration='WebBehavior' bindingConfiguration='WebConfiguration' contract='FileUploadApplication.FileUploadWcfService.IKendoFileUploadService' /> |
</service> |
</services> |
</system.serviceModel> |
Step – 2
Then in the service contract, we have to add an operation which accepts a stream for its only parameter.
[ServiceContract] |
public interface IKendoFileUploadService |
{ |
[OperationContract] |
void SaveFile(Stream file); |
} |
Step – 3
In this step, we have to define the operation contract and accepts all the files as stream coming from the client via kendo file upload.
public class KendoFileUploadService : IKendoFileUploadService |
{ |
[WebInvoke(Method = 'POST', BodyStyle = WebMessageBodyStyle.Bare)] |
public void SaveFile(Stream stream) |
{ |
var fileNameWithContents = GetFileNameWithContents(stream, Encoding.UTF8); |
File.WriteAllBytes(fileNameWithContents.Key, fileNameWithContents.Value); |
} |
private byte[] GetByteFromStream(Stream stream) |
{ |
using (var memoryStream = new MemoryStream()) |
{ |
stream.CopyTo(memoryStream); |
return memoryStream.ToArray(); |
} |
} |
private KeyValuePair<string, byte[]> GetFileNameWithContents(Stream stream, Encoding encoding) |
{ |
var fileNameWithContents = new KeyValuePair<string, byte[]>(); |
var fileBytes = GetByteFromStream(stream); |
// Copy to a string for header parsing |
string content = encoding.GetString(fileBytes); |
// The first line should contain the delimiter |
int delimiterEndIndex = content.IndexOf('rn'); |
if (delimiterEndIndex <= -1) |
{ |
} |
else |
{ |
string delimiter = content.Substring(0, content.IndexOf('rn')); |
// Look for Content-Type |
Regex re = new Regex(@'(?<=Content-Type:)(.*?)(?=rnrn)'); |
Match contentTypeMatch = re.Match(content); |
// Look for filename |
re = new Regex(@'(?<=filename=')(.*?)(?=')'); |
Match filenameMatch = re.Match(content); |
// Did we find the required values? |
if (contentTypeMatch.Success && filenameMatch.Success) |
{ |
var fileName = filenameMatch.Value.Trim(); |
// Get the start & end indexes of the file contents |
int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + 'rnrn'.Length; |
byte[] delimiterBytes = encoding.GetBytes('rn' + delimiter); |
int endIndex = IndexOf(fileBytes, delimiterBytes, startIndex); |
int contentLength = endIndex - startIndex; |
// Extract the file contents from the byte array |
byte[] fileData = new byte[contentLength]; |
Buffer.BlockCopy(fileBytes, startIndex, fileData, 0, contentLength); |
var contents = fileData; |
fileNameWithContents = new KeyValuePair<string, byte[]>(fileName, fileData); |
} |
} |
return fileNameWithContents; |
} |
private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex) |
{ |
int index = 0; |
int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex); |
if (startPos != -1) |
{ |
while ((startPos + index) < searchWithin.Length) |
{ |
if (searchWithin[startPos + index] serachFor[index]) |
{ |
index++; |
if (index serachFor.Length) |
{ |
return startPos; |
} |
} |
else |
{ |
startPos = Array.IndexOf<byte>(searchWithin, serachFor[0], startPos + index); |
if (startPos -1) |
{ |
return -1; |
} |
index = 0; |
} |
} |
} |
return -1; |
} |
} |
Step – 4
After we are done with all code set up, then now simply we have to call the SaveFile method of contract via kendo file upload from our jquery.
Suppose I have following control in my aspx page and in the js file, we have to write the following code and we are done. It will upload multiple files asynchronously to server.
$('#files').kendoUpload({ |
multiple: true, |
async: { |
saveUrl: '/FileUploadWcfService/KendoFileUploadService.svc/SaveFile', |
removeUrl: 'remove', |
autoUpload: false |
} |
}); |
Conclusion