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(Conform sTo = WsiProfiles.BasicProfile1_ 1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Service s.ScriptSe rvice]
public class Service : System.Web.Services.WebSer vice
{
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(docbin aryarray, 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(documen tcontents, 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(mybytea rray, 0, len);
localhost.Service myservice = new localhost.Service();
myservice.SaveDocument(myb ytearray, sFile.Remove(0, sFile.LastIndexOf("\\") + 1));
objfilestream.Close();
}
}
}
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(Conform
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Service
public class Service : System.Web.Services.WebSer
{
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(docbin
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(documen
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(mybytea
localhost.Service myservice = new localhost.Service();
myservice.SaveDocument(myb
objfilestream.Close();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.