How to upload a file to another server

Source: Internet
Author: User

Assume there are two servers: A and B, where A is a Web server (ip:192.123.1.1, a server for IIS publishers), and B is a file server (ip:192.123.2.2)

    1. Create a shared folder under a disk in file Server B docshare, and create a user on that server, such as Docuser, and assign this user read and write access to the shared folder
    2. The same user, such as Docuser, is also created in Web Server A, and the password must be consistent with the Docuser in B. and add user Docuser to the IIS_WPG user group
    3. Create a new application pool Docpool, and Set permissions authentication, enter user name Docuser and password.
    4. Set IIS published program's application pool to Docpool
    5. Add a virtual directory under the root of the program that is published by IIS, the name is files, and the virtual directory maps to the shared directory path of server B, such as: \\192.123.2.2\DocShare set a specific user access, enter the user name Docuser, password.
    6. In the program code, the save path to the settings file is Server.MapPath ("Files ") : The path to the virtual directory.

Http://www.cnblogs.com/guangpeng/archive/2013/04/09/3009600.html

In the program deployment often encountered a problem, in order to security, speed, is generally the database and the program is separated on different servers. Generally one server is enough, other if you want to external, through this external server can be achieved.

I was just working on a project. A problem has been encountered, the program is located in a small server space, and the acquisition of a data server (storage database and files). In the program, many places to use the attachment upload, the general upload files are placed in the program directory. But this time it's different, put it on another server.
At first I thought it was very simple, as long as the network mapping is not OK? As long as there is sufficient permissions, and the operation of the machine disk what is the difference?

I've done this before, using. NET WinForm to summarize Excel, which was not a problem at the time. This time is WebForm, the problem comes, the procedure can not find at all.
This good, think of a lot of aspects or no solution, in the Internet did not search, so in the CSDN sent a request post, finally resolved.
http://community.csdn.net/Expert/topic/5470/5470471.xml?temp=.5512812

The summary is as follows:

Simple test the virtual directory settings are correct! Suppose a is your Web site Server B is the server that you want to upload files 1, share a folder on the B server upload, and set permissions to open the user test password 123456 and give test to upload write permissions, while in the folder into a simple page, Anything can be done, output a few letters on the "B Server Operation over" 2, in a server Web site under the establishment of a virtual directory, the default add virtual directory when there is no choice of the other server's shared folder, you choose a local path, set up well, Click on the properties of the virtual directory, on the change of the local path, there are 3 choices, you select "Another computer sharing", and then set the network path, the format is: \\{server}\{share name},{server} with IP instead, {share name} is the upload folder, After filling in, click "Connect User" in the back, enter the user test and password on the B server 123456 (of course, you'd better test it first, to make sure that a server can access the folder B server share, if a is not able to access B share, then you first solve this problem first, Test method is very simple, in the browser input \\b server ip\upload, if you can enter the user and password can see you just built the file, OK, sharing is accessible) 3, the virtual directory is set up, you can test it, Under A's web site through the program to obtain a virtual directory path, if successful, the path should be Server B's Ip+upload folder. 4, the next job is to upload files, this time you directly upload should be able to, should you have just entered Access B server upload account number and password, that is, A and B has established a trust connection, but if you restart the server, the connection just established does not exist. So in the a server Web site Global.asax file to add a sentence, let a server site start with B to establish a trust connection System.Diagnostics.Process.Start ("Net.exe", "Use \\\\  b server's ip\\upload \ "123456\"/user:\ "test\" "); The above statement is to establish a trust connection, the benefit of the Global.asax is that the site started to establish a connection, because this trust connection is sustainable, and will not expire like a session, only when restarting a server, this trust connection will disappear. I don't know, do you understand? But I can be sure that this is achievable because I just did this piece. Something: As to how the files uploaded to the b server are accessed, itIt's more convenient. One way: or through a server Web site under the virtual directory to access, in this case, B server is just another way to provide space: on the B server to establish a site, point to the directory, so B server shared access to the file when IIS work tasks

File Upload Save:
SaveAs (Server.MapPath ("abc") + "\ \" + filepath.substring (filepath.lastindexof ("\ \") + 1));
ABC is the virtual directory under which the program runs the site or virtual directory

File browsing:
DownloadFile (Server.MapPath ("abc") + "//abc.txt");
Abc.txt as a file on a B server

Private voidDownloadFile (stringPhysicalfilepath) {FileStream Stream=NULL; Try{Stream=NewFileStream (Physicalfilepath, FileMode.Open, FileAccess.Read, FileShare.Read); intBufSize = (int) stream.                Length; byte[] buf =New byte[BufSize]; intBytesread = stream. Read (BUF,0, bufSize); HttpContext.Current.Response.ContentType="Application/octet-stream"; //attachment is downloaded as an attachment, or it can be changed online. HttpContext.Current.Response.AppendHeader ("content-disposition","attachment;filename="+Httputility.urlencode (System.IO.Path.GetFileName (Physicalfilepath), System.Text.Encoding.UTF8)); HttpContext.Current.Response.OutputStream.Write (BUF,0, Bytesread);            HttpContext.Current.Response.End (); }            finally{stream.            Close (); }        } 

Http://www.cnblogs.com/cnaspnet/archive/2008/09/23/716881.html

Try to do it yourself:

First, ensure that the intranet site server can access the file server.

1. Create a user on the file server for example: User name: Uploaduser, Password: 123456

2. Create a shared folder on the file server for example: DATA_QZ also increases user Uploaduser read and Write permissions for this folder.

3. Create a user with the same name and password on the Web server.

4. Restart the Web site for IIS to run.

Note: The node Uploadpath needs to be created under the <appSettings> node in the Webconfig file. For example: <add key= "Uploadpath" value= "\ \ File server ip\ shared folder \"/>, such as: "\\192.168.0.7\Data_qz\".

webconfig:     <add key="uploadpath" value="\\192.xxx.0.x\Data_qz\"/>    <add key="uploaduser" value="uploaduser"/>    <add key="uploadpwd" value="Aa123456 " />

Global.asax

voidApplication_Start (Objectsender, EventArgs e) { //code to run when the application startsSystem.Diagnostics.Process.Start ("Net.exe","Use \ \"+configurationmanager.appsettings["Uploadpath"]. Trim ()+" \""+configurationmanager.appsettings["uploadpwd"]. Trim () +"\ "/user:\""+configurationmanager.appsettings["Uploaduser"]. Trim () +"\""); }

page upload code: // upload f_upload locally . SaveAs (Request.mappath (". /data/appraise"@"\" +// offsite upload
F_upload. SaveAs (configurationmanager.appsettings["Uploadpath"). Trim () + @ "data\\appraise\\" + AnnounceDoc.FileUrl.Trim ());

//Foreground control
<asp:fileupload runat="Server"Id="F_upload"/>page upload code://Local uploadsF_upload. SaveAs (Request.mappath (".. /data/appraise") +@"\"+AnnounceDoc.FileUrl.Trim ()); //Offsite uploadsF_upload. SaveAs (configurationmanager.appsettings["Uploadpath"]. Trim () +@"data\\appraise\\"+ AnnounceDoc.FileUrl.Trim ());

How to upload a file to another server

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.