Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
7 Feb 2013 1  
Upload an image on one server and save it on another.

Introduction

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();
        doc.LoadXml(sb.ToString());
        localhost.ImageWebService ws = new localhost.ImageWebService();
        ws.SaveImage(sb.ToString(), myFile.FileName.ToString(), fileLength);
    }
    else
    {
        txtPath.Text = "error: please check the path";
    }

}

Webservice project code

[WebMethod()]
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)
{
    try
    {
        // 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
        _FileStream.Close();

        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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here