Link to home
Start Free TrialLog in
Avatar of Tra71
Tra71

asked on

Send and receive multiple binary documents using ASP.Net

Hi,

I've located and applied code to send and receive binary documents using Visual Studio 2010.  I want to be able to receive multiple files and test.  I will be receiving one pdf document as a byte[] and photos as some form a collection of byte[].  Any help would be appreciated.

**Web Service**

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



[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
     
    [WebMethod]
    public bool SaveDocument(Byte[] docbinaryarray, string docname)
    {
        string strdocPath;
        strdocPath = "C:\\DocumentDirectory\\" + 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 DocumentName)
    {
        string strdocPath;
        strdocPath = "C:\\DocumentDirectory\\" + DocumentName;

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

        return len;
    }


    [WebMethod]
    public Byte[] GetDocument(string DocumentName)
    {
        string strdocPath;
        strdocPath = "C:\\DocumentDirectory\\" + DocumentName;

        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;
    }

}

**Test Code**

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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string sFile = @"C:\IMG_0212.jpg";

        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.Service myservice = new localhost.Service();
            myservice.SaveDocument(mybytearray, sFile.Remove(0, sFile.LastIndexOf("\\") + 1));
            objfilestream.Close();
        }

           }
}
ASKER CERTIFIED SOLUTION
Avatar of madgino
madgino
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial