Because of the large number of files uploaded and deleted in the project, considering the security factors, the whole idea is to upload the file resources to the file server using FTP from the main server.
FTP upload to the server code as follows (a simple addition, a lot of specific online)
public static void UploadFile (FileInfo FileInfo, string hostname, string Username, string password) { string target; String targetDir = DateTime.Now.ToString ("Yyyy-mm-dd"); Create file directory MakeDir (Targetdir,hostname,username,password); target = Guid.NewGuid (). ToString (); String URL = "ftp://" + hostname + "/" + TargetDir + "/" + target; FtpWebRequest ftp = getrequest (URL, username, password); Ftp.method = WebRequestMethods.Ftp.UploadFile; Ftp.usebinary = true; Ftp.usepassive = true; Ftp.contentlength = Fileinfo.length; const int buffersize = 2048; Byte[] Content=new byte[buffersize]; int dataread; using (FileStream fs=fileinfo.openread ()) {try {using (Strea M rs = Ftp.getrequeststream ()) {do {dataread = fs. Read (content, 0, buffersize); Rs. Write (content, 0, dataread); } while (! ( Dataread < buffersize)); Rs. Close (); }} catch (Exception) {throw; } finally {fs. Close (); Console.WriteLine ("Upload success"); FTP = getrequest (URL, username, password); Ftp.method = System.Net.WebRequestMethods.Ftp.Rename; Renamed Ftp.renameto =target+ fileInfo.Name.Substring (fileInfo.Name.IndexOf ('. ')); try {ftp.getresponse (); } catch (Exception ex) {ftp = Getrequest (URL, username, password); Ftp.method = System.Net.WebRequestMethods.Ftp.DeleteFIle Delete Ftp.getresponse (); Throw ex; } finally {//fileinfo. Delete (); }//can record a log "upload" + FileInfo. FullName + "Upload" + "ftp://" + hostname + "/" + TargetDir + "/" + FileInfo. Name + "success."); FTP = NULL; } }
File upload to the server is very simple, next to the browser's address to display the file (here is mainly to do image server), in fact, you can build a website, and then through the site's virtual directory to access the picture, but the personal feeling this is certainly not a good solution, So think of the recent comparison of the fire Nginx to do the image agent.
Install Nginx, I encountered a small problem during the installation (Windows can not find the file Nginx ... This error is because the location is not positioned correctly, I use the nginx-1.9.0 version, I just locate the installation files in the parent directory, so there is this error, if you have a similar error, the simplest way is to find the Nginx installation program, address bar address directly stick out on the line.
Another problem is that port 80 is occupied, and when we have configured IIS, the default port is occupied by the default Web site of IIS, so you only need to modify the default Web site port of IIS.
After the installation is successful, there will be two processes in Task Manager
Next, enter Loaclhost in the Address bar, and you will notice that there will be
Welcome to nginx!
After the Nginx installation is successful, modify the nginx.conf file.
Location/{ root html; Index index.html index.htm; } location/image{ #当访问Image folder when specifying the map to the real physical address of alias J:\FTPFinCloud; #默认的图片index Default.png;} #缓存图片location ~.*. (gif|jpg|jpeg|png|bmp|swf) ${expires 10d;}
Then execute the NGINX-T, and then execute the nginx-s reload after success. This is the time to access the picture.
Because I'm using the local, so my address is http://192.168.1.218/Image/2015-05-17/2015-05-17.jpg,
What to do if you need to bind a domain name when you want to publish to a remote server. Just configure it inside the server
server{ listen 8080; server_name your_server_ip; Location/ { root/; } Access_log/usr/local/webserver/nginx/logs/nginx_access.log;}}
You can change the you_server_ip.
Use Nginx to do file server on Windows Server2012