Asp.net cross-Server File Upload Solution

Source: Internet
Author: User
Why do we need multi-server storage?

One rule for Web Front-end optimization is to download resources (parallelize downloads using SS hostnames) in parallel on different hosts. In this case, the IE browser can only download two files for the same domain name, if there are many resources in the same page station, especially when there are too many image resources, the page loading speed will be affected. In this case, distributed file processing is particularly important, medium and large websites often store these files through multiple resource servers. For example, the blog garden itself Stores image files on the imgn.cnblogs.com server. For larger websites, some JS and CSS files may be separated from the primary domain.

Cross-Server Upload Solution

Cross-Server Upload is to upload files to Site B on Site A. The following two methods are commonly used.

The first method is very flexible. Assume a.com and B .com, there is a file gate on. aspx. When there is no parameter on this page, the page will jump to the upload on B. aspx, in fact, the upload is through upload on B. upload aspx to server B, and pass the upload result as a parameter to gate. aspx; gate. aspx receives this parameter and then processes it accordingly. In essence, this method does not involve cross-server, but only uses some flexible alternatives.

The second method is the actual cross-Server Upload that we will mention today. Using the B .com handler file, we first encode the file and use the POST method to pass it to handler, then handler saves the file to the server and returns the corresponding result.

Convert the file to base64:

String filename = fileupload. filename; fileinfo file = new fileinfo (filename); string ext = file. extension; stream = fileupload. postedfile. inputstream; binaryreader BR = new binaryreader (Stream); byte [] filebyte = BR. readbytes (INT) stream. length); string basefilestring = convert. tobase64string (filebyte );

Handle in handler. ashx:

/// <Summary> // upload an image /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] public class crossserverfileupload: ihttphandler {private string _ allowfiletypes = "GIF, JPG, JPEG, PNG, BMP"; private httpcontext _ context; Public void processrequest (httpcontext context) {_ context = context; string src = context. request. form ["src"]; if (String. isnullorempty (SRC) return; src = SRC. tolower (); bool isextvalid = SRC. endswith (". jpg ") | SRC. endswith (". PNG ") | SRC. endswith (". GIF ") | SRC. endswith (". BMP ") | SRC. endswith (". JPEG "); If (isextvalid) {context. response. contenttype = "text/plain"; string ext = context. request. form ["Ext"]; string content = context. request. form ["data"]; upload (EXT, content) ;}/// <summary> // upload class /// </s Ummary> /// <Param name = "Ext"> </param> // <Param name = "content"> </param> private void upload (string Ext, string content) {If (string. isnullorempty (EXT) | string. isnullorempty (content) {return;} // Save the image string datenum = datetime. now. tostring ("yyyymm"); // string filename = GUID is stored on a monthly basis. newguid () + ext; string currentpath = httpcontext. current. request. physicalapplicationpath + "Upload \" + datenum + "\"; String fullpath = currentpath + filename; If (! Directory. exists (currentpath) directory. createdirectory (currentpath); byte [] buffer = convert. frombase64string (content); Using (filestream = new filestream (fullpath, filemode. create) {filestream. write (buffer, 0, buffer. length);} string host = _ context. request. URL. host; responsewrite (string. format ("http: //" + host + "/upload/{0}/{1}", datenum, filename )); // return the image storage path} // <summary> // generate HTTP Content /// </Summary> /// <Param name = "content"> </Param> private void responsewrite (string content) {_ context. response. clearcontent (); _ context. response. contenttype = "text/plain"; _ context. response. write (content); _ context. response. end ();}

Cross-server processingCodeThe core code, but also the permission issue needs to be considered in actual applications.

Code download

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.