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