After the establishment of the good one webservice there will be a self-bringing
[webmethod]//writes this in all the methods that will be written, which is easy to debug Public string HelloWorld () { return"HelloWorld"; }
Now you can try the entry record.
[WebMethod] PublicUserInfo Login (stringUserName,stringpwd) { if(UserName = ="Admin"&& pwd = ="123") { return NewUserInfo () {username="Admin", pwd="", age= -, remark="I'm very handsome ." }; } Else { return NULL; } }
Called in the controller in the MVC project
//First step: Add a service reference//Instantiate Service Reference: service object to SoapClientMywebservicesoapclient client =Newmywebservicesoapclient (); PublicActionResult Index () {stringresult =client. HelloWorld (); Response.Write (Result); returnView (); }
[WebMethod]
Public UserInfo Login (string userName, string pwd)
{
if (UserName = = "Admin" && pwd = = "123")
{
return new UserInfo () {username= "admin", pwd= "", age=50,remark= "I'm Handsome"};
}
Else
{
return null;
}
}
Then you can write a simple file upload download
Public classMyWebService:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello World"; } /// <summary> ///Uploading Files/// </summary> /// <param name= "bytes" ></param> /// <param name= "FileName" ></param> /// <returns></returns>[WebMethod] Public BOOLFileUpload (byte[] Bytes,stringfileName) { Try { //instantiates a Memory object MemoryStream, loads a byte array into a Memory objectMemoryStream memory =NewMemoryStream (bytes); //File Save path stringFilePath = Server.MapPath ("~/files/"+fileName); //instantiating a File objectFileStream FStream =NewFileStream (FilePath, FileMode.OpenOrCreate); //writing a Memory object to a file objectmemory. WriteTo (FStream); //Releasing Objectsmemory. Close (); Memory. Dispose (); Fstream.close (); Fstream.dispose (); return true; } Catch(Exception ex) {return false; }} [WebMethod] Public byte[] FileDownload (stringFileName) { //Load Path stringFilePath = Server.MapPath ("~/files/"+FileName); //instantiates a file object and reads the specified fileFileStream fs =File.openread (FilePath); intB1; //instantiating a Memory objectSystem.IO.MemoryStream Tempstream =NewSystem.IO.MemoryStream (); //iterate through the file and convert the file to byte[] while((B1 = fs.) ReadByte ())! =-1) {Tempstream.writebyte ( (byte) (B1)) ; } returnTempstream.toarray (); } [WebMethod] PublicList<filemanager>getfilelist () {//Load Path stringFilePath = Server.MapPath ("~/files/"); //instantiate the DirectoryInfo and load the specified pathDirectoryInfo directory =NewDirectoryInfo (FilePath); List<FileManager> result =NewList<filemanager>(); //facilitates all folders under the specified path foreach(DirectoryInfo Iteminchdirectory. GetDirectories ()) {FileManager temp=NewFileManager (); Temp. FileName=item. Name; Temp. FilePath=item. FullName; Temp. FileType=1; Result. ADD (temp); } //traverse all files under the specified path foreach(FileInfo Iteminchdirectory. GetFiles ()) {FileManager Temp2=NewFileManager (); Temp2. FileName=item. Name; Temp2. FilePath=item. FullName; Temp2. FileType=2; Result. ADD (TEMP2); } returnresult; } }
Called in a controller in an MVC project
PublicActionResult FileUpload () {returnView (); } [HttpPost] Publicactionresult FileUpload (httppostedfilebase file1) {Stream FileStream=File1. InputStream; byte[] bytes =New byte[Filestream.length]; FileStream.Read (Bytes,0, Bytes. Length); //sets the position of the current stream as the start of the streamFilestream.seek (0, Seekorigin.begin); BOOLresult =client. FileUpload (bytes, file1. FileName); if(Result) {Response.Write ("File Upload Successful! "); } Else{Response.Write ("File upload failed! "); } returnView (); } PublicActionResult filedownload () {List<FileManager> result =client. Getfilelist (). ToList (); StringBuilder SB=NewStringBuilder (); Sb. Append ("<ul>"); foreach(varIteminchresult) {sb. Append (string. Format ("<li><a href= '/home/filedownload/{0} ' >{1}</a></li>", item. FilePath, item. FileName)); } sb. Append ("</ul>"); Viewbag.filelist=sb. ToString (); returnView (); } [HttpPost] Publicactionresult filedownload (formcollection coll) {string[] s = {"1","3" }; foreach(varIteminchs) {byte[] result =client. FileDownload (item); //encoding of the output streamResponse.Charset ="UTF-8"; Response.ContentEncoding= System.Text.Encoding.GetEncoding ("UTF-8"); //output Type is stream fileResponse.ContentType ="Application/octet-stream"; Response.AddHeader ("content-disposition","attachment; Filename="+"new text document. txt"); Response.BinaryWrite (result); Response.Flush (); Response.End (); } return NewEmptyresult (); }
Write poorly, may be wrong please understand, wrong please point out
. NET Enterprise Application WebService upload download file