A small case of C # NET deployment Picture Distributed Storage Server

Source: Internet
Author: User

If the Web service users more, access more, users upload pictures, files and other content put in a piece, presumably the server is unable to withstand, this time, we need to consider the method of distributed storage.

: A Web server drag 2 image servers

How to do the user uploaded pictures more evenly stored on two pictures----simple can think of random numbers

Here, let's give a demonstration based on random numbers;

Designing a database for storage scenarios

To have a table to hold the picture server information, there is a table to store the image path;

VS2013 created in the project directory, you see the creation of two ImageService general handler, since the picture server to receive the Web server send picture request, then to implement its program, deployed in 2 image server program is actually the same, Here you need to demonstrate two picture servers on a single computer, so you've created multiple programs

First the model layer uses EF to generate directly from the database

2. Picture server handler use the general handler here ashx

First add a reference to the model

Then create a new generic handler fileup.ashx, two image servers are the same

Namespace Qau. Dfs.    imageservice1{//<summary>//Fileup Summary description///</summary> public class Fileup:ihttphandler {public void ProcessRequest (HttpContext context) {context.            Response.ContentType = "Text/plain"; int serverid = Int. Parse (context.            request["ServerID"]); String ext = context.            request["ext"];            String dir = "/image/" + DateTime.Now.Day + "/"; Directory.CreateDirectory (Path.getdirectoryname) (context.            Request.mappath (dir)); String newfilename = Guid.NewGuid ().            ToString ();            String fulldir = dir + newfilename + ext; using (FileStream FileStream = file.openwrite (context. Request.mappath (Fulldir)) {context.                Request.InputStream.CopyTo (FileStream);                Imageinfo imageinfo = new Imageinfo ();                Imageinfo.imagename = Fulldir;                Imageinfo.imageserverid = ServerID; Imageserverentities db = nEW imageserverentities (); Db.                Imageinfo.add (Imageinfo); Db.            SaveChanges ();            }} public bool IsReusable {get {return false; }        }    }}

3, the new Web project Dfs.web, using the empty MVC4,

The new home controller code is as follows:

HomeController.cs

public class Homecontroller:controller {////GET:/home/imageserverentities db = new Imageser        Verentities ();        Public ActionResult Index () {return View ();            } public ActionResult Fileup () {httppostedfilebase file = request.files["Fileup"]; String fileName = Path.getfilename (file.            FileName);            String fileext = Path.getextension (fileName); if (Fileext = = ". png" | |  fileext== ". jpg") {//filter out the available Picture server collection from the Server state table as C, and get the total number of records N of the collection, then generate a random number R1 with a random function, and use R1 and N to take the remainder operation as i=r1%n. C[i] is the picture server to save the picture list<imageserverinfo> List = db. Imageserverinfo.where (c = c.flgusable = = True).                ToList (); int count = list.                Count ();                Random random = new random (); int r = Random.                Next ();                int i = r% Count;                Imageserverinfo imageservice = List[i]; String URL = imageservice.serverurl;//Image server address int serverid = imageservice.serverid;//Picture Server number String a                ddress = "http://" +url+ "/fileup.ashx?serverid=" +serverid+ "&ext=" +fileext;                WebClient client = new WebClient (); Client. Uploaddata (Address, streamtobyte (file.                            InputStream));                    } return Content ("OK"); } private byte[] Streamtobyte (Stream stream) {byte[] buffer = new Byte[stream.            Length]; Stream. Read (buffer, 0, buffer.            Length); Stream.            Seek (0, Seekorigin.begin);        return buffer; Public ActionResult ShowImage () {var imageserverinfo = db. Imageserverinfo.where (c = c.flgusable = = True).            ToList ();            viewdata["imageserverlist"] = Imageserverinfo;        return View (); }    }}

  

4. Upload page index.cshtml

<div>        <form method= "POST" action= "/home/fileup" enctype= "Multipart/form-data" >            <input type= "File" name= "Fileup"/>            <input type= "Submit" value= "Upload"/>        </form>    </div>

5, display the uploaded pictures

Showimage.cshtml

<div>        
Related Article

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.