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.
One: 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 {///
#Region Component Designer Generated Code // Web Service Designer The private icontainer components = NULL;
///
#ndregion
Public class images: system.web.services.Webservice {///
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 postcode, getBinaryFileShow.aspx.cs file content is as follows: use 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 {///
Private void page_load (Object sender, system.eventargs e) {// Place the user code here to initialize the page // definition and initialize the file object; XML.SZ.LuoHuedu.Net.aspxwebcs.getBinaryFile.Images Oimage; Oimage = New xml.sz.luohuedu.net.aspxwebcs.getBinaryFile.Images (); // gets a binary file byte array; byte [] image = oimage.getiMage ("); //) To support storage area for memory Stream system.io.MemoryStream MemStream = new system.io.MemoryStream (image); // definition and instantiate Bitmap object Bitmap BM = new bitmap (memstream); // // / / 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"; //////////////////////////////bit. You can actually decide according to the situation. Response.addheader ("Content-Disposition", "Attachment; FileName = OK.jpg");} elseresponse.contentType = Oimage.GetImageType (); response.binarywrite (image); response.end (); r
#Region Web Form Designer Generated CodeOverride Protected Void OnNit (Eventargs E) {//// Codegen: This call is required for the ASP.NET Web Form Designer. //Initializecomponent();Base.oninit (E);
///
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"%>