C # upload images to WebService during development

Source: Internet
Author: User

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.

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.