Users Online

We have 138 guests online
SOAP File Upload PDF Print E-mail
Written by Zack MIlls   
Friday, 09 July 2010 13:27


using System;

using System.Data;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.ComponentModel;

using System.IO; 


namespace Uploader


    /// <summary>

    /// This web method will provide an web method to load any

    /// file onto the server; the UploadFile web method

    /// will accept the report and store it in the local file system.

    /// </summary>

    [WebService(Namespace = "")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


    public class FileUploader : System.Web.Services.WebService





public string UploadFile(byte[] f, string fileName)


    // the byte array argument contains the content of the file

    // the string argument contains the name and extension

    // of the file passed in the byte array



        // instance a memory stream and pass the

        // byte array to its constructor

        MemoryStream ms = new MemoryStream(f);


        // instance a filestream pointing to the

        // storage folder, use the original file name

        // to name the resulting file

        FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath

                    ("~/TransientStorage/") +fileName, FileMode.Create); 

        // write the memory stream containing the original

        // file as a byte array to the filestream


        // clean up




        // return OK if we made it this far

        return "OK";


    catch (Exception ex)


        // return the error message if the operation fails

        return ex.Message.ToString();





/// <summary>

/// Upload any file to the web service; this function may be

/// used in any application where it is necessary to upload

/// a file through a web service

/// </summary>

/// <param>Pass the file path to upload</param>

private void UploadFile(string filename)




        // get the exact file name from the path

        String strFile = System.IO.Path.GetFileName(filename);


        // create an instance fo the web service

        TestUploader.Uploader.FileUploader srv = new



        // get the file information form the selected file

        FileInfo fInfo = new FileInfo(filename);


        // get the length of the file to see if it is possible

        // to upload it (with the standard 4 MB limit)

        long numBytes = fInfo.Length;

        double dLen = Convert.ToDouble(fInfo.Length / 1000000);


        // Default limit of 4 MB on web server

        // have to change the web.config to if

        // you want to allow larger uploads

        if (dLen < 4)


            // set up a file stream and binary reader for the

            // selected file

            FileStream fStream = new FileStream(filename,

            FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fStream);


            // convert the file to a byte array

            byte[] data = br.ReadBytes((int)numBytes);



            // pass the byte array (file) and file name to the web service

            string sTmp = srv.UploadFile(data, strFile);




            // this will always say OK unless an error occurs,

            // if an error occurs, the service returns the error message

            MessageBox.Show("File Upload Status: " + sTmp, "File Upload");




             // Display message if the file was too large to upload

             MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");



    catch (Exception ex)


         // display an error message to the user

         MessageBox.Show(ex.Message.ToString(), "Upload Error");



Last Updated on Friday, 09 July 2010 13:31