Click here to Skip to main content
13,404,627 members (37,156 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 25 Oct 2009

Uploading Large Files Through Web Service

, 25 Oct 2009
Rate this:
Please Sign up or sign in to vote.
Uploading large files through web service method that will accept chunks of bytes


In this article, I am going to explain how to upload large files using web service method. We will not need to change anything in IIS or web.config (maximum request size) developed in Visual Studio 2008.

Using the Code

I have created a web method to accept file name, bytes (array) and offset. Here is the function.

/// <summary>
/// This function is used to append chunk of bytes to a file name. 
/// If the offset starts from 0 means file name should be created.
/// </summary>
/// <param name="FileName">File Name</param>
/// <param name="buffer">Buffer array</param>
/// <param name="Offset">Offset</param>
/// <returns>boolean: true means append is successfully</returns>
public bool UploadFile(string FileName, byte[] buffer, long Offset)
bool retVal = false;
// setting the file location to be saved in the server. 
// reading from the web.config file 
string FilePath = 
	Path.Combine(ConfigurationManager.AppSettings["upload_path"], FileName);

if (Offset == 0) // new file, create an empty file
// open a file stream and write the buffer. 
// Don't open with FileMode.Append because the transfer may wish to 
// start a different point
using (FileStream fs = new FileStream(FilePath, FileMode.Open, 
	FileAccess.ReadWrite, FileShare.Read))
fs.Seek(Offset, SeekOrigin.Begin);
fs.Write(buffer, 0, buffer.Length);
retVal = true;
catch (Exception ex)
//sending error to an email id
return retVal;

In the function, I am setting the file location to be saved in the server. I have put it in a appSetting section in the web.config file.

<add key="upload_path" value="d:\files\"></add>

How to Consume the Web Method

I have created a Windows application (C#). I have put a button. On the button click, I have put the following code:

//the file that we want to upload
string FilePath="d:\somefile.pdf";
int Offset = 0; // starting offset.

//define the chunk size
int ChunkSize = 65536; // 64 * 1024 kb

//define the buffer array according to the chunksize.
byte[] Buffer = new byte[ChunkSize];
//opening the file for read.
FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
//creating the ServiceSoapClient which will allow to connect to the service.
WSservice.ServiceSoapClient soap_client = new WSservice.ServiceSoapClient();
long FileSize = new FileInfo(FilePath).Length; // File size of file being uploaded.
// reading the file.
fs.Position = Offset;
int BytesRead = 0;
while (Offset != FileSize) // continue uploading the file chunks until offset = file size.
BytesRead = fs.Read(Buffer, 0, ChunkSize); // read the next chunk 
	// (if it exists) into the buffer. 
	// the while loop will terminate if there is nothing left to read
// check if this is the last chunk and resize the buffer as needed 
// to avoid sending a mostly empty buffer 
// (could be 10Mb of 000000000000s in a large chunk)
if (BytesRead != Buffer.Length)
ChunkSize = BytesRead;
byte[] TrimmedBuffer = new byte[BytesRead];
Array.Copy(Buffer, TrimmedBuffer, BytesRead);
Buffer = TrimmedBuffer; // the trimmed buffer should become the new 'buffer'
// send this chunk to the server. it is sent as a byte[] parameter, 
// but the client and server have been configured to encode byte[] using MTOM. 
bool ChunkAppened = soap_client.UploadFile(Path.GetFileName(FilePath), Buffer, Offset);
if (!ChunkAppened)

// Offset is only updated AFTER a successful send of the bytes. 
Offset += BytesRead; // save the offset position for resume
catch (Exception ex)


  • 25th October, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Gulf air - Bahrain
Bahrain Bahrain
Current Position is : Information System Analyst

Leading a Team of Developers for developing ecommerce websites and systems integration using several integration methods such as XML.

Manage ISP infrastructure, web servers, database servers, application servers, email servers and networks. Design the architecture of complex web applications; build web applications in JAVA, asp, asp.Net; manage web sites including networking and database; experienced in MS Sql Server and Oracle;

stay current with emerging trends and technological advances in the industry.

MCTS,MCAD VB.NET, ASP.NET and XML Services,BTEC National Diploma in Computer Studies.

You may also be interested in...


Comments and Discussions

PraiseMy 5 STARS Pin
Santosh Kokatnur30-May-16 5:16
memberSantosh Kokatnur30-May-16 5:16 
PraiseMy 5***** Pin
Santosh Kokatnur30-May-16 5:16
memberSantosh Kokatnur30-May-16 5:16 
QuestionCan this code be modified somehow to incorporate Asynchronous Programming? Pin
Member 117078571-Jun-15 23:20
memberMember 117078571-Jun-15 23:20 
QuestionAbsolutely fantastic !!! Pin
don_nell22-Jun-13 15:27
memberdon_nell22-Jun-13 15:27 
QuestionMy vote of 5 Pin
George Serbanescu18-Mar-13 2:19
memberGeorge Serbanescu18-Mar-13 2:19 
GeneralMy vote of 4 Pin
Pallenov6-Feb-13 10:58
memberPallenov6-Feb-13 10:58 
QuestionDeployment of solution Pin
Laurentiu LAZAR5-Dec-12 0:22
memberLaurentiu LAZAR5-Dec-12 0:22 
Questionregarding namespace to be included Pin
vijayalaya7-Nov-12 21:39
membervijayalaya7-Nov-12 21:39 
GeneralMy vote of 4 Pin
Harshit Raj Singh21-Oct-12 12:35
memberHarshit Raj Singh21-Oct-12 12:35 
QuestionAn attempt was made to move the file pointer before the beginning of the file Pin
tungkhac11-Oct-12 0:15
membertungkhac11-Oct-12 0:15 
AnswerRe: An attempt was made to move the file pointer before the beginning of the file Pin
don_nell22-Jun-13 15:35
memberdon_nell22-Jun-13 15:35 
QuestionReg. Upload files which are of 10 GB Pin
Erukulla vijay18-Dec-11 19:36
memberErukulla vijay18-Dec-11 19:36 
GeneralMy vote of 5 Pin
Luiz Carvalho13-Dec-11 19:13
memberLuiz Carvalho13-Dec-11 19:13 
GeneralMy vote of 4 Pin
Ramanjaneyulu Kondaru8-Nov-11 2:16
memberRamanjaneyulu Kondaru8-Nov-11 2:16 
GeneralGreat! Pin
Kantabriko17-Nov-10 15:25
memberKantabriko17-Nov-10 15:25 
QuestionCould you post a sample project? Pin
Rui Frazao17-Jun-10 0:39
memberRui Frazao17-Jun-10 0:39 
GeneralHi Pin
Ravenet27-Oct-09 16:06
memberRavenet27-Oct-09 16:06 
GeneralRe: Hi Pin
hussain.attiya27-Oct-09 20:31
memberhussain.attiya27-Oct-09 20:31 
GeneralRe: Hi Pin
Ravenet28-Oct-09 4:48
memberRavenet28-Oct-09 4:48 
GeneralRe: Hi Pin
Ravenet28-Oct-09 4:53
memberRavenet28-Oct-09 4:53 
GeneralMy vote of 1 Pin
Mark Nischalke26-Oct-09 6:43
mentorMark Nischalke26-Oct-09 6:43 
GeneralRe: My vote of 1 Pin
hussain.attiya27-Oct-09 0:14
memberhussain.attiya27-Oct-09 0:14 
AnswerWeb services are converting bytes array to 64 base Pin
ErezAlster25-Oct-09 5:22
memberErezAlster25-Oct-09 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1802120.2 | Last Updated 25 Oct 2009
Article Copyright 2009 by hussain.attiya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid