Web Service: save images to the image server on the client

Source: Internet
Author: User

1. Create a service

Asmx is relatively simple: The uploaded image is passed in as a base64string Parameter

Note: This conversion is lossy. The JPEG file is converted to base64string, and then converted back to a jpeg file, which is significantly smaller than the source image (I found this was the case during the test .)

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. Web;
Using system. Web. Services;
Using system. IO;
Using system. text;

Namespace childpictureupload
{
/// <Summary>
/// Summary description for uploadpic.
/// </Summary>
[WebService (namespace = "http://xxx.xxx/ChildPictureUpload/")]
Public class childpictureupload: system. Web. Services. WebService
{
Public childpictureupload ()
{
Initializecomponent ();
}

# Region component designer generated code

// Required by the Web Services designer
Private icontainer components = NULL;

/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
}

/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion


[Webmethod]
Public bool uploadpictrue (string filecontent, string filename)
{
If (filecontent. Length = 0)
{
Return false;
}
Else
Try
{
Byte [] B = system. Convert. frombase64string (filecontent );
Filename = system. configuration. configurationsettings. deleetfolder ["uploadfolder"] + filename;
String Path = filename. substring (0, filename. lastindexof ("//"));
System. Io. Directory. createdirectory (PATH );
System. io. filestream Sw = new filestream (filename, system. io. filemode. openorcreate, system. io. fileaccess. readwrite, system. io. fileshare. none, 4096, false );
Sw. Write (B, 0, B. Length );
Sw. Close ();
Return true;
}
Catch
{
Return false;
}
}
}
 
}
 

To facilitate deployment, configure the upload address in Web. config:

<Add key = "uploadfolder" value = "C:/inetpub/wwwroot/childpictures/"/>

The ASPnet user permission must be added to this directory (otherwise, how can I store image files ?)

2. Call the service to upload images

Page code:

// Save picture to remote server
Childpictureupload upload = new childpictureupload ();
Stream M = This. filephoto. postedfile. inputstream;
Byte [] B = new byte [M. Length];
M. Seek (0, system. Io. seekorigin. Begin );
Int I = M. Read (B, 0, (INT) M. Length );
String S = system. Convert. tobase64string (B );
If (! Upload. uploadpictrue (S, filename ))
{
Messaging. showmessage (this. Page, "the picture has not been uploaded with some error.", messaging. emmessagetype. messagetype_error );
}
Else {..}

3. webserver deployment

Web. config: configure the service address and image directory

<Add key = "uploadserviceurl" value = "http: // 192.168.1.4/childpictureupload/childpictrueupload. asmx"/>

<Add key = "childphotoimagepath" value = "childphotos"/>

In IIS Site configuration, map the image directory to the image storage directory on the image server

After debugging, it passes.

In fact, the format and size of the uploaded image should be limited.

 

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.