Upload and download files in WebService

Source: Internet
Author: User

Not much to say, look directly at the code:

/*WebService of uploading files*/usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingsystem.web;usingSystem.Web.Services;usingSystem.IO;/// <summary>///a summary description of the Upload. /// </summary>[WebService (Namespace ="http://tempuri.org/", Description="use the. NET Framework to upload files in Web services. ")] Public classuploadfile:system.web.services.webservice{ PublicUploadFile () {}[webmethod (Description="The Web service provides a method that returns whether the file upload succeeds or not. ")] Public stringUpload (byte[] FS,stringFileType) {stringFileName = System.DateTime.Now.ToString ("yyyymmddhhmmssms") +"."+FileType;Try{///defines and instantiates a memory stream to hold a byte array that is submitted. MemoryStream m =NewMemoryStream (FS);///define the actual file object and save the uploaded file. FileStream f =NewFileStream (Server.MapPath (".") +"\\UploadFile\\"+FileName, FileMode.Create);///writes the data in the internal memory to the physical fileM.writeto (f); M.close (); F.close (); F=NULL; M=NULL;returnFileName;}Catch(Exception ex) {return NULL;}} [WebMethod (Description="methods provided by the Web service to delete the specified file")] Public voidDelete (stringfileName) {stringFilePath = Server.MapPath (".") +"\\UploadFile\\"+FileName; File.delete (FilePath);}}/*Download File*/usingSystem;usingsystem.web;usingSystem.Collections;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.IO;/// <summary>///a summary description of the getbinaryfile. ///WEB Services Name: Getbinaryfile///function: Returns a binary byte array of a file object on the server. /// </summary>[WebService (Namespace ="http://tempuri.org/", Description="the. NET Framework is used in Web services to deliver binaries. ")] Public classgetbinaryfile:system.web.services.webservice{/// <summary>///The Web Service provides a method that returns a byte array for a given file. /// </summary>[WebMethod (Description ="A method provided by the WEB service that returns a byte array of the given file")] Public byte[] GetFile (stringrequestfilename) {stringFileName = Server.MapPath (".") +"\\UploadFile\\"+Requestfilename;returnGetbinaryfile (fileName);}/// <summary>///getbinaryfile: Returns the byte array of the given file path. /// </summary>/// <param name= "filename" ></param>/// <returns></returns> Public byte[] Getbinaryfile (stringfilename) {if(file.exists (filename)) {Try{///Open the existing file for reading. FileStream s =file.openread (filename);returnConvertstreamtobytebuffer (s); S.close ();}Catch(Exception e) {return New byte[0];}}Else{return New byte[0];}}/// <summary>///Convertstreamtobytebuffer: Converts the given file stream to a binary byte array. /// </summary>/// <param name= "Thestream" ></param>/// <returns></returns> Public byte[] Convertstreamtobytebuffer (System.IO.Stream thestream) {intB1; System.IO.MemoryStream Tempstream=NewSystem.IO.MemoryStream (); while((B1 = Thestream.readbyte ())! =-1) {Tempstream.writebyte ( (byte) (B1)) ;}returnTempstream.toarray ();} [WebMethod (Description="the method provided by the WEB service that returns the given file type. ")] Public stringGetimagetype () {///This is just a test, you can dynamically output according to the actual file typereturn "image/jpg";}}/*Invoke upload*/stringFilePath ="c:\test.jpg"; FileStream FS=NewFileStream (FilePath, filemode.openorcreate,fileaccess.read);//Read the Data into the Byte Arraybyte[] Filebyte =New byte[FS. Length];fs. Read (Filebyte,0, (int) fs. Length); UploadFile UploadFile=NewUploadFile ();intIndexOf = Filepath.lastindexof (".")+1;stringFileext = filepath.substring (indexof, Filepath.length-indexof);stringfilename = UploadFile. Upload (Filebyte, Fileext);

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.