/* Upload the file's WebService */
Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. Web;
Using system. Web. Services;
Using system. IO;
/// <Summary>
/// Summary of upload.
/// </Summary>
[WebService (namespace = "http://tempuri.org /",
Description = "Use the. NET Framework in web services to upload files. ")]
Public class uploadfile: system. Web. Services. WebService
{
Public uploadfile ()
{
}
[Webmethod (description = "method provided by the web service, returns whether the file is uploaded successfully or not. ")]
Public String upload (byte [] FS, string filetype)
{
String filename = system. datetime. Now. tostring ("yyyymmddhhmmssms") + "." + filetype;
Try
{
/// Define and instantiate a memory stream to store the submitted byte array.
Memorystream M = new memorystream (FS );
/// Define the actual object and save the uploaded object.
Filestream F = new filestream (server. mappath (".") + "\ uploadfile \\"
+ Filename, filemode. Create );
/// Write data in the memory to a physical file
M. writeto (f );
M. Close ();
F. Close ();
F = NULL;
M = NULL;
Return filename;
}
Catch (exception ex)
{
Return NULL;
}
}
[Webmethod (description = "methods provided by the web service, delete a specified file")]
Public void Delete (string filename)
{
String filepath = server. mappath (".") + "\ uploadfile \" + filename;
File. Delete (filepath );
}
}
/* Download the object */
Using system;
Using system. Web;
Using system. collections;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. IO;
/// <Summary>
/// Summary of getbinaryfile.
/// Web Services Name: getbinaryfile
/// Function: return the binary byte array of a file object on the server.
/// </Summary>
[WebService (namespace = "http://tempuri.org /",
Description = "Use the. NET Framework in web services to transmit binary files. ")]
Public class getbinaryfile: system. Web. Services. WebService
{
/// <Summary>
/// The method provided by the web service, returns the byte array of the given file.
/// </Summary>
[Webmethod (description = "the method provided by the web service, returns the byte array of the given file")]
Public byte [] GetFile (string requestfilename)
{
String filename = server. mappath (".") + "\ uploadfile \" + requestfilename;
Return getbinaryfile (filename );
}
/// <Summary>
/// Getbinaryfile: returns the byte array of the given file path.
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Returns> </returns>
Public byte [] getbinaryfile (string filename)
{
If (file. exists (filename ))
{
Try
{
/// Open an existing file for reading.
Filestream S = file. openread (filename );
Return convertstreamtobytebuffer (s );
S. Close ();
}
Catch (exception E)
{
Return new byte [0];
}
}
Else
{
Return new byte [0];
}
}
/// <Summary>
/// Convertstreamtobytebuffer: convert a given file flow into a binary byte array.
/// </Summary>
/// <Param name = "thestream"> </param>
/// <Returns> </returns>
Public byte [] convertstreamtobytebuffer (system. Io. Stream thestream)
{
Int B1;
System. Io. memorystream tempstream = new system. Io. memorystream ();
While (b1 = thestream. readbyte ())! =-1)
{
Tempstream. writebyte (byte) B1 ));
}
Return tempstream. toarray ();
}
[Webmethod (description = "method provided by the web service, returns the given file type. ")]
Public String getimagetype ()
{
/// This is only a test. You can perform dynamic output based on the actual file type.
Return "image/jpg ";
}
}
/* Call upload */
String filepath = "C: \ test.jpg ";
Filestream FS = new filestream (filepath, filemode. openorcreate,
Fileaccess. Read );
// Read the data into the byte array
Byte [] filebyte = new byte [fs. Length];
FS. Read (filebyte, 0, (INT) fs. Length );
Uploadfile = new uploadfile ();
Int indexof = filepath. lastindexof (".") + 1;
String fileext = filepath. substring (indexof, filepath. Length-indexof );
String filename = uploadfile. Upload (filebyte, fileext );