Click here to Skip to main content
12,764,784 members (37,517 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 3 Feb 2013

How to upload an image on one server and save it on another using ASP.NET and web services

, 7 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Upload an image on one server and save it on another.


There can be a need to upload images or text files on one server and save them on another. This article will help you achieve such a task using ASP.NET and web services.

Using the code

There would be two separate projects to cover this query. One project for a website and another for a web service using VS 2008 or VS 2010. I am assuming you would have created both projects.

There is a simple logic in achieving this target. We will browse the file from one server and save it on the destination using a web service hosted on the destination server.

Website Project

  1. Place the control on an ASPX page - you need a Browse button, Save button, and a textbox to show the notifications or errors. 
  2. Under the Save button click event, place the below code. Please note: you need to add a reference of the web service you will make in another project. I have explained below the details about the web service.
  3. The browsed file InputStream is converted into XML using byte[] using serialization.
  4. The myData byte[] object is serialized ser.Serialize(wr, myData); using a string builder and string writer. The string is loaded into XML.
  5. This XML is passed as one of the parameters under the web service hosted on another project. You can pass as many parameters as needed based on your requirements.
protected void btnSaveImage_Click(object sender, EventArgs e)
    string UploadFileFolderPath = "C:\\Intel";
    System.IO.Stream MyStream;

    if (btnBrowse.PostedFile != null)
        HttpPostedFile myFile = btnBrowse.PostedFile;

        int fileLength = myFile.ContentLength;

        byte[] myData = new byte[fileLength];
        myFile.InputStream.Read(myData, 0, fileLength);
        // Gor in Binary Data
        XmlSerializer ser = new XmlSerializer(myData.GetType());


        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter wr = new System.IO.StringWriter(sb);
        ser.Serialize(wr, myData);
        XmlDocument doc = new XmlDocument();
        localhost.ImageWebService ws = new localhost.ImageWebService();
        ws.SaveImage(sb.ToString(), myFile.FileName.ToString(), fileLength);
        txtPath.Text = "error: please check the path";


Webservice project code

public string SaveImage(string imageStreamPath, string imageName, int fileLength)
    string result = "";
    string UploadFileFolderPath = "C:\\Ankit";

    byte[] dsXML = new byte[fileLength];

    System.IO.StringReader read = new StringReader(imageStreamPath);

    System.Xml.XmlReader reader = new XmlTextReader(read);
    XmlSerializer ser = new XmlSerializer(dsXML.GetType());
    object obj = ser.Deserialize(reader);
    dsXML = (byte[])obj;

    ByteArrayToFile(imageName, dsXML);

    return result;

public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
        // Open file for reading
        _FileName = "C:\\Users\\ankitjain.AVALONSOLUTIONS\\Documents\\" + 
          "visual studio 2010\\Projects\\WebServices\\WebServices\\"  + _FileName;

        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, 
                 System.IO.FileMode.Create, System.IO.FileAccess.Write);

        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

        // close file stream

        return true; 
    catch (Exception _Exception)
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());

    // error occured, return false
    return false;

Webservice Project

Paste the above code under your ASMX page.

  1. Create a method SaveImage(string imageStreamPath, string imageName, int fileLength).
  2. The imageStreamPath contains the Browse File InputStream as an XML. I have deserialized it. Please note: you need the fileLength parameter.
  3. Here we need an XML reader and string reader instead of an XML load and string writer.
  4. The ByteArrayToFile method converts the deserialized file path stream and saves it to the destination folder.

Please note: both projects will go parallel. You need to add a reference of the web service to your website project.


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionReg/- uplode image Pin
Member 1141381028-Mar-15 22:08
memberMember 1141381028-Mar-15 22:08 
GeneralXML for binary data? Pin
John Brett4-Feb-13 3:00
memberJohn Brett4-Feb-13 3:00 
GeneralEditing Pin
DaveAuld4-Feb-13 1:06
protectorDaveAuld4-Feb-13 1:06 

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.170217.1 | Last Updated 7 Feb 2013
Article Copyright 2013 by ContactAnkitJain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid