Web services allows you to upload and download files.

Source: Internet
Author: User

With the development of Internet technology and the increasing demand for cross-platform services, Web Services is widely used. We need to pass string information through web services and binary file information. Next, we will introduce how to download files from the server to the client through Web Services and upload files from the client to the server through Web Services.

1. Display and download files through Web Services

The Web Services created here are named getbinaryfile, which provides two common methods: getimage () and getimagetype (). The former returns the binary file byte array, and the latter returns the file type, the getimage () method has a parameter used to select the name of the file to be displayed or downloaded on the client. The files we show and downloaded here can not be in the virtual directory. The advantage of this method is that the files can be displayed and downloaded according to the permissions. We can see from the following method that, the actual file location is not in the virtual directory, so you can better control the permissions of the file, which is particularly useful for high security. This feature is available in the previous ASPProgramCan be implemented using stream objects. AllSource codeAnd in the sourceCode.

First, create the getbinaryfile. asmx file:

We can. create a C # aspxwebcs project in. net, add a new project, select "Web service", and set the file name to getbinaryfile. asmx: Enter the following code in "view code": getbinaryfile. asmx. CS:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. Web;
Using system. Web. UI;
Using system. Web. Services;
Using system. IO;

Namespace xml.sz.luohuedu.net. aspxwebcs
{
/// <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://xml.sz.luohuedu.net /",
Description = "Use the. NET Framework in web services to transmit binary files. ")]
Public class getbinaryfile: system. Web. Services. WebService
{

# Region component designer generated code
// Required by the Web Service designer
Private icontainer components = NULL;

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

Public class images: 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 [] getimage (string requestfilename)
{
/// Get an image of the server
/// If you test it by yourself, make sure to modify the actual physical path below
If (requestfilename = NULL | requestfilename = "")
Return getbinaryfile ("D: \ picture. jpg ");
Else
Return getbinaryfile ("D: \" + requestfilename );
}

/// <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 );
}
Catch (exception E)
{
Return new byte [0];
}
}
Else
& Nbsp ;{
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 ";
}
}
}
}

 

Once the above asmx file is created and compiled, We can compile the client code to call this web services.

First, "add web reference" and enter: http: // localhost/aspxwebcs/getbinaryfile. asmx. Next, we will write the intermediate file of the display file: getbinaryfileshow. aspx. Here, we only need to write the code in the subsequent code. The content of the getbinaryfileshow. aspx. CS file is as follows:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Web. Services;

Namespace aspxwebcs
{
/// <Summary>
/// Summary of getbinaryfileshow.
/// </Summary>
Public class getbinaryfileshow: system. Web. UI. Page
{

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
/// Define and initialize the object;
Xml.sz.luohuedu.net. aspxwebcs. getbinaryfile. Images oimage;
Oimage = new xml.sz.luohuedu.net. aspxwebcs. getbinaryfile. Images ();
/// Obtain the byte array of the binary file;
Byte [] image = oimage. getimage ("");
/// Convert to a stream that supports the storage zone as the memory
System. Io. memorystream memstream = new system. Io. memorystream (image );
/// Define and instantiate a bitmap object
Bitmap Bm = new Bitmap (memstream );
/// Output or download according to different conditions;
Response. Clear ();
/// If the request string is specified for download, the object will be downloaded;
/// Otherwise, it will be displayed in the browser.
If (request. querystring ["Download"] = "1 ")
{
Response. Buffer = true;
& N

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.