WebService uploads images

Source: Internet
Author: User

WebService

/// <Summary>
/// Abstract description of the uploaded image Webserver
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Toolboxitem (false)]
Public class webservice1: system. Web. Services. WebService
{
[Webmethod]
Public bool updatefile (byte [] content, string pathand, string filename)
{
String pathandname = pathand + filename;
Int Index = pathandname. lastindexof (".");
If (Index = 0)
{
Return false;
}
Else
{
String extended = string. empty;
If (index + 1 = pathandname. length)
{
Return false;
}
Else
{
Extended = pathandname. substring (index + 1 );
If (Extended = "Jpeg" | extended = "GIF" | extended = "jpg" | extended = "BMP" | extended = "PNG")
{
Try
{
If (! Directory. exists (@ pathand) // create a folder if the folder does not exist.
{
Directory. createdirectory (@ pathand); // create a folder
}

// File. writeallbytes (server. mappath (pathandname), content );
File. writeallbytes (pathandname, content );
Return true;
}
Catch (exception ex)
{
Return false;
}
}
Else
{
Return false;
}
}
}
}
}

// Test

Private void btnsaveserver_click (Object sender, eventargs E)
{
Openfiledialog filedialog = new openfiledialog ();
If (filedialog. showdialog () = dialogresult. OK)
{
String pathand = commonclass. config. getappsettings <string> ("productimageurl", @ "D: \ fsterp \ productimage \");
String imagename = "mylove ";
Bool uploadresult = uploadimagewebservice (filedialog. filename, pathand, imagename );
If (uploadresult)
MessageBox. Show ("Upload successful! ");
Else
MessageBox. Show ("Upload Failed! ");
}
}
/// <Summary>
/// Upload an image [through webserver]
/// </Summary>
/// <Param name = "FILENAME"> select the image path [default file inclusion suffix] </param>
/// <Param name = "pathand"> upload the server folder [Create if the folder does not exist] </param>
/// <Param name = "imagename"> name of the uploaded image file [excluding the suffix] </param>
/// <Returns> upload result </returns>
Public bool uploadimagewebservice (string filename, string pathand, string imgname)
{

String extension = path. getextension (filename). tolower (). Replace (".","");
String paramsuffix = "|" + commonclass. config. getreceivettings <string> ("imageformat", "JPG | jpge | GIF | BMP | PNG") + "| ";
Int Pi = paramsuffix. indexof ("|" + extension + "| ");
If (pI <0)
{
MessageBox. Show ("only JPG files can be uploaded | jpge | GIF | BMP | images in PNG format! ");
Return false;
}
Else
{
Fileinfo = new fileinfo (filename );
If (fileinfo. length> 20480)
{
MessageBox. Show ("the uploaded image cannot exceed 20 K ");
}
Else
{
// Stream file = filedialog. openfile ();
Filestream file = new filestream (filename, filemode. Open, fileaccess. Read );
Byte [] bytes = new byte;
File. Read (bytes, 0, bytes. Length );
// Instantiate the WebService. Servicereference1 is the namespace we set when adding a reference
WebService. webservice1 WebService = new fsterp. WebService. webservice1 ();
Datetime time = datetime. now;
// Rename the image name and Path
// String pathand = commonclass. config. getdeleeturl <string> ("productimageurl", @ "D: \ fsterp \ productimage \");
String imagename = imgname + "." + extension;
// String pathandname = pathand + imagename;
If (WebService. updatefile (bytes, pathand, imagename ))
{
Return true;
}
Else
{
Return false;
}
}
}
Return false;
}


Test Image


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.