Web. config configuration code in the WebService Project
<Deleetask>
<Add key = "uploadfilefolder" value = "/uploads/testupload/"/>
</Appsettings>
Imageservice. asmx code in the WebService Project
/// <Summary>
/// Download file handler in WebService
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Returns> returned file stream </returns>
[Webmethod (description = "download server site file, transfer file relative path")]
Public byte [] downloadfile (string strfilepath)
{
Filestream FS = NULL;
String currentuploadfolderpath = server. mappath (configurationmanager. deleetfolder ["uploadfilefolder"]);
String currentuploadfilepath = currentuploadfolderpath + strfilepath;
If (file. exists (currentuploadfilepath ))
{
Try
{
/// Open an existing file for reading.
FS = file. openread (currentuploadfilepath );
Int B1;
System. Io. memorystream tempstream = new system. Io. memorystream ();
While (b1 = FS. readbyte ())! =-1)
{
Tempstream. writebyte (byte) B1 ));
}
Return tempstream. toarray ();
}
Catch (exception ex)
{
Return new byte [0];
}
Finally
{
FS. Close ();
}
}
Else
{
Return new byte [0];
}
}
Code of the form Download button in the winform Project
Private void btndownload_click (Object sender, eventargs E)
{
String currentservicefilepath = this.txt servicefile. Text. Trim ();
String currentdownloadfolderpath = this.txt downloadfolder. Text. Trim ();
If (currentservicefilepath = "" | currentdownloadfolderpath = "")
{
MessageBox. Show (downloadimage (currentservicefilepath, currentdownloadfolderpath ));
}
Else if (currentservicefilepath = "")
{
MessageBox. Show ("Enter the path of the server file to be downloaded and select the local storage directory ");
}
Else if (currentdownloadfolderpath = "")
{
MessageBox. Show ("Enter the path of the server file to be downloaded and select the local storage directory ");
}
}
The form Download button in the winform project calls the function.
/// <Summary>
/// Download an object through WebService
/// </Summary>
/// <Param name = "servicefilepath"> server image path </param>
/// <Param name = "downloadfolderpath"> Local Image path </param>
Private string downloadimage (string servicefilepath, string downloadfolderpath)
{
Try
{
String downloadfilename = "";
If (servicefilepath. Contains ("/"))
{
Downloadfilename = servicefilepath. substring (servicefilepath. lastindexof ("/"));
}
Else
{
Downloadfilename = servicefilepath;
}
String downloadfilepath = downloadfolderpath + "\" + downloadfilename;
Localhost. imageservice myimageservice = new localhost. imageservice ();
Byte [] bytes = myimageservice. downloadfile (servicefilepath );
If (Bytes! = NULL)
{
If (! Directory. exists (downloadfolderpath )){
Directory. createdirectory (downloadfolderpath );
}
If (! File. exists (downloadfilepath ))
{
File. Create (downloadfilepath). Dispose ();
}
// Create an upload path if no complete upload path exists.
Fileinfo downloadinfo = new fileinfo (downloadfilepath );
If (downloadinfo. isreadonly) {downloadinfo. isreadonly = false ;}
// Define and instantiate a memory stream to store the submitted byte array.
Memorystream MS = new memorystream (bytes );
// Define the actual object and save the uploaded object.
Filestream FS = new filestream (downloadfilepath, filemode. Create );
/// Write data in the memory to a physical file
Ms. writeto (FS );
FS. Flush ();
Ms. Flush ();
Ms. Close ();
FS. Close ();
FS = NULL;
MS = NULL;
}
Return "Download successful ";
}
Catch (exception ex)
{
Return "Download failed" + ex. message;
}
}