C # WebService File Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.