Instance description:
WebService is more and more widely used. The client program (Traffic Police) needs to upload daily report problems to the Web server. The report problems can contain images, but uploading image files occupies a large amount of resources, therefore, a separate webmethod is used for multimedia upload and download.
[Webmethod] Public String uploadmedia (string functionname, byte [] BS) {string S = "2"; try {tools TT = new tools (); s = TT. savemedia (functionname, BS); If ("1 ". equals (s) {S = "0"; // a data update row indicates that the data is successfully saved} else {S = "1" ;}} catch (exception e) {console. writeline (E. tostring ();} return s ;}
Code Description:
The webmethod uploadmedia method is used to upload images and other multimedia files. The parameter is functionname and the byte stream of images. After uploading a multimedia file, save it to the multimedia shared directory and add the index to the database table.
Determine whether the upload is successful based on the number of rows affected by database operations. If yes, 0 is returned.
/// <Summary> /// function: save the multimedia file and write it to the database /// </Summary> /// <Param name = "functionname"> taskid01_jpg </param> /// <Param name = "filestream"> multimedia file stream </param> /// <returns> returns the number of affected rows </returns> Public String savemedia (string functionname, byte [] BS) {string returnrow = "0"; try {// Save the multimedia file string [] temp = functionname. split ('_'); string id = temp [0]. tostring (); string name = temp [1]. tostring (); string userid = TEM P [0]. tostring (). substring (8, 3); string filename = "E: \ shares \" + ID + ". "+ name; fileinfo = new fileinfo (filename); If (file. exists (filename) {file. delete (filename) ;}if (! Fileinfo. directory. exists) {fileinfo. directory. create ();} filestream FS = new filestream (filename, filemode. createnew, fileaccess. write, fileshare. none, BS. length, false); FS. write (BS, 0, BS. length); FS. close (); // modify the data table string SQL = "insert into media (ID, name, userid) values ('" + ID + "', '" + name + "', '"+ userid +"') "; getdatafromdb = new getdatafromdb (); returnrow = getdatafromdb. updateservicedatasfromtable (SQL);} catch (exception e) {console. writeline (E. tostring ();} return returnrow ;}
Code Description:
Save the multimedia file and add the data to the database table.