File Upload Web Service

|

This is a File Upload Web Service that can be a wrapper for a process in between.
Source code is an adaptation of this Microsoft article: HOW TO: Send and Receive Binary Documents by Using an ASP.NET Web Service and Visual C# .NET

Upload Client
UploadClient

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UploadClient
{
    public partial class Form1 : Form
    {
        public static string sFile = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            FileStream objfilestream = new FileStream(sFile, FileMode.Open, FileAccess.Read);
            int len = (int)objfilestream.Length;
            Byte[] mybytearray = new Byte[len];
            objfilestream.Read(mybytearray, 0, len);
            localhost.Service1 myservice = new localhost.Service1();

           myservice.SaveDocument(mybytearray, sFile.Remove(0, sFile.LastIndexOf("")));
            objfilestream.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MemoryStream objstreaminput = new MemoryStream();
            FileStream objfilestream = new FileStream(sFile.Insert(sFile.LastIndexOf("."), "_processed"), FileMode.Create, FileAccess.ReadWrite);

            sFile = sFile.Replace(@"\", @"");

			//MessageBox.Show(sFile.Replace(@"", @"") + Environment.NewLine + sFile.Remove(0, sFile.LastIndexOf("") + 1));

            localhost.Service1 myservice = new localhost.Service1();
            int len = myservice.GetDocumentLen(sFile.Remove(0, sFile.LastIndexOf("") + 1));
            Byte[] mybytearray = new Byte[len];
            mybytearray = myservice.GetDocument(sFile.Remove(0, sFile.LastIndexOf("") + 1));
            objfilestream.Write(mybytearray, 0, len);
            objfilestream.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Browse for file to parse

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:";
            openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Put filename and path in textbox1
                sFile = openFileDialog1.FileName.Replace(@"",@"");
                txtInputFile.Text = sFile;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace FileTransferWebService
{
	/// <summary>
	/// Summary description for Service1
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	public class Service1 : System.Web.Services.WebService
	{

		[WebMethod]
		public string HelloWorld()
		{
			return "Hello World";
		}

        [WebMethod]
        public bool SaveDocument(Byte[] docbinaryarray, string docname)
        {
            string strdocPath;
            strdocPath = "c:temp" + docname;
            FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.ReadWrite);
            objfilestream.Write(docbinaryarray, 0, docbinaryarray.Length);
            objfilestream.Close();

            return true;
        }

        [WebMethod]
		public int GetDocumentLen(string docname)
        {
            string strdocPath;
			strdocPath = "c:temp" + docname;

            FileStream objfilestream = new FileStream(strdocPath, FileMode.Open, FileAccess.Read);
            int len = (int)objfilestream.Length;
            objfilestream.Close();

            return len;
        }


        [WebMethod]
		public Byte[] GetDocument(string docname)
        {
            string strdocPath;
			strdocPath = "c:temp" + docname;

            FileStream objfilestream = new FileStream(strdocPath, FileMode.Open, FileAccess.Read);
            int len = (int)objfilestream.Length;
            Byte[] documentcontents = new Byte[len];
            objfilestream.Read(documentcontents, 0, len);
            objfilestream.Close();

            return documentcontents;
        }

	}
}
Originally Posted on March 28, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.