With the development of Internet technology and the increasing cross-platform demand, Web Services is getting wider and wider, but we need to deliver string information through Web Services, but also need to pass binary information. Below, we will show how to download files from the server from the server from the server to the client from the server to the server from the server.
First, display and download files via web services
The name of the web services established here is GetBinaryFile, providing two public methods: getImage () and getImageType (), the former returns the binary byte array, the latter returns the file type, where the getImage () method has a parameter Used to select the file name to display or download at the client. The file we have displayed and downloaded can not be in the virtual directory, the benefits of using this method are: can display and download control according to the permissions, from the following method. We can see that the actual file location is not in the virtual directory. Under, the file can be better permissible to the file, which is particularly useful in the case of high security. This feature can be implemented in a STREAM object in the previous ASP program. In order to facilitate the reader to test, all source code is listed here, and the introduction and comment in the source code.
First, establish a getBinaryFile.asmx file:
We can create a new C # ASPXWEBCS project in vs.net, then "Add new item", select "Web Service", and set the file name as: getBinaryFile.asmx, enter the following code in View Code, ie: 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
{
///
/// getBinaryFile's summary description. /// Web Services Name: getBinaryFile /// Function: Returns the binary byte array of file objects on the server. ///
[WebService (Namespace = "http://xml.sz.luohuedu.net/",
Description = "Use the .NET framework in Web Services to pass binary.")]
Public class getBinaryFile: System.Web.Services.WebService
{
#Region Component Designer Generated Code
/ / Web service designer necessary
Private icontainer Components = NULL;
///
/// Clean all the resources being used. ///
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing && Components! = NULL)
{
Components.dispose ();
}
Base.dispose (Disposing);
}
#ndregion
Public class images: system.web.services.Webservice {
///
/// Web service provides the method of returning the byte array of a given file. ///
[WebMethod (Description = "WEB service provides the method of return to a given file")]
Public Byte [] GetImage (String RequestFileName)
{
// get a picture of the server side
// If you test it yourself, pay attention to modify the following actual physical path
if (RequestFileName == Null || RequestFileName == "")
Return GetBinaryFile ("D: //Picture.jpg");
Else
Return getBinaryFile ("D: //" RequestFileName);
}
///
/// GetBinaryFile: Returns an array of bytes for the file path. ///
///
///
Public Byte [] getBinaryFile (String filename)
{
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
{
Return New Byte [0];
}
}
///
/// ConvertStreamTobytebuffer: Converts a given file stream to a binary byte array. ///
///
///
Public Byte [] ConvertStreamTobytebuffler (System.io.Stream Free)
{
INT B1;
System.io.MemoryStream TempStream = new system.io.MemoryStream ();
While ((b1 = theretream.readbyte ())! = - 1)
{
TempStream.writebyTe ((Byte) B1));
}
Return TempStream.toArray ();
}
[WebMethod (Description = "Web service provides a given file type.")]]]
Public string getImageType ()
{
/// This is just a test, you can perform dynamic output according to the actual file type
Return "image / jpg";
}
}
}
}
Once we have created the above ASMX file, we can write this web services after compiling.
Let's first "add Web reference", type: http://localhost/aspxwebcs/getbinaryfile.asmx. Below, we write the intermediate file displaying the file: getBinaryFileSpo.aspx, here, we only need to write code in the post-code code, getBinaryFileShow.aspx.cs file content 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
{
///
/// getBinaryFileShow's summary description. ///
Public class getBinaryFileSHow: system.web.ui.page
{
Private Void Page_Load (Object Sender, System.EventArgs E)
{
/ / Place the user code here to initialize the page
// / Define and initialize the file object;
Xml.sz.luohuedu.net.aspxwebcs.getbinaryfile.images Oimage;
Oimage = new xml.sz.luohuedu.net.aspxwebcs.getbinaryfile.Images ();
/// Get a binary file byte array;
BYTE [] image = Oimage.GetImage ("");
/// Convert to support the storage area for the flow
System.io.MemoryStream Memstream = New System.io.MemoryStream (image);
// / definition and instantiate Bitmap objects
Bitmap BM = New Bitmap (Memstream);
// / Output or download according to different conditions;
Response.clear ();
/// If the request string specifies the download, download the file;
// / Otherwise, it is displayed in the browser.
IF (Request.QueryString ["Download"] == "1")
{
Response.buffer = true;
Response.contenttype = "Application / OcTet-stream";
/// The file name OK.jpg download and output here is an example. You can actually decide according to the situation.
Response.addheader ("Content-Disposition", "Attachment; FileName = OK.jpg");
}
Else
Response.contentType = Oimage.GetImageType ();
Response.binarywrite (image);
Response.end ();
}
#Region Web Form Designer Generated Code
Override protected void oninit (Eventargs E)
{
//
// Codegen: This call is necessary for the ASP.NET Web Form Designer.
//
InitializationComponent ();
Base.onit (E);
}
///
/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///
Private vidinitiRizeComponent ()
{
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
}
}
Finally, we write the final browsing page: getBinaryFile.aspx, this file is simple, only the aspx file is required, the content is as follows:
<% @ Page language = "c #" codebehind = "getBinaryfile.aspx.cs" autoeventwireup = "false" inherits = "aspxwebcs.getbinaryfile"%>%>%>%>%>%>%>%>%>%>%>
Display and download files via Web Services