Click here to Skip to main content
12,885,022 members (29,301 online)
Click here to Skip to main content


112 bookmarked
Posted 2 Oct 2010

Saving Files Into Database Using .NET Web Services

, 3 Oct 2010 CPOL
In this article, I will show you how to save big files into database using .NET Web Service and how to monitor transfer status.
Service References
Web References
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FileTransfer
    public partial class frmMain : Form
        public frmMain()

        private void frmMain_Load(object sender, EventArgs e)


        private void cmdSelectFile_Click(object sender, EventArgs e)
            //Using OpenFileDialog we can browse thorugh file system
            //and select a file, we want to save into to database.
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.Title = "Select a file";
            txtFile.Text= (dialog.ShowDialog() == DialogResult.OK)
               ? dialog.FileName : null;

        private void cmdSave_Click(object sender, EventArgs e)
                //now we have to set value of pbTransferSatatus to 0.
                pbTransferStatus.Value = 0;
                //Before saving we must create an instance of FileTransfer class, 
                //which allows us use 3 methods mentioned earlyer.
                FileTransferWebService.FileTransfer ft =
                    new FileTransfer.FileTransferWebService.FileTransfer();

                //GetTemFilename() method creates temporary file on server and returns it's name.
                string tempFileName = ft.GetTempFilename();
                FileInfo fi = new FileInfo(txtFile.Text);
                using (FileStream fs = new FileStream(txtFile.Text,FileMode.Open,FileAccess.Read))
                    //using BinaryReader we will read blocks of data from file
                    //and append then to the temporary file.
                    using (BinaryReader br = new BinaryReader(fs))
                        fs.Position = 0;
                        int numBytesToRead = (int)fs.Length;
                        int numBytesRead = 0;
                        while (numBytesToRead > 0)
                            //I choose 100 000 bytes long blocks.
                            byte[] b = br.ReadBytes(100000);
                            ft.AppendToTempFile(tempFileName, b);
                            if (b.Length == 0)
                            numBytesRead += b.Length;
                            numBytesToRead -= b.Length;
                            //When block is saved, than we can update pbTransferStatus value
                            int progressStatus = (int)((double)100 / (double)fs.Length * (double)numBytesRead);
                            pbTransferStatus.Value = progressStatus;
                        ft.SaveFileIntoDatabase(Path.GetFileName(txtFile.Text), tempFileName);
                        MessageBox.Show("File has been saved into database", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            catch (Exception err)
                MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Kanasz Robert
Architect The Staffing Edge & Marwin Cassovia Soft
Slovakia Slovakia
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist

Open source projects: DBScripter - Library for scripting SQL Server database objects

Please, do not forget vote

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 3 Oct 2010
Article Copyright 2010 by Kanasz Robert
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid