On the FTP server, I added some Chinese welcome words to the "messages" tab.
BelowCodeAfter debugging, the system prompts "the basic connection has been closed; An error occurred while receiving". After querying relevant information, I can find the following methods to solve this problem.
Method: remove the Chinese welcome word from the FTP server or change it to an English welcome word. This is a Microsoft Bug.
Code:
Public void upload (string filename)
{
// ip
string ftpip = system. web. configuration. webconfigurationmanager. deleetpipeline ["ftpip"];
// user name
string ftpusername = system. web. configuration. webconfigurationmanager. appsettings ["ftpusrname"];
// User Password
string ftppassord = system. web. configuration. webconfigurationmanager. deleetask[ "ftpusrpsw"];
fileinfo fileinf = new fileinfo (filename);
string uri = "ftp: // "+ ftpip +"/sys/"+ fileinf. name;
ftpwebrequest reqftp;
// Create an ftpwebrequest object based on the URI
Reqftp = (ftpwebrequest) ftpwebrequest. Create (New uri (URI ));
// FTP user name and password
Reqftp. Credentials = new networkcredential (ftpusername, ftppassord );
// The default value is true, and the connection will not be closed
// Executed after a command
Reqftp. keepalive = false;
// Specify the command to be executed
Reqftp. method = webrequestmethods. FTP. uploadfile;
// Specify the data transmission type
Reqftp. usebinary = true;
// Notify the server of the file size when uploading the file
Reqftp. contentlength = fileinf. length;
// Set the buffer size to 2 kb.
Int bufflength = 2048;
Byte [] buff = new byte [bufflength];
Int contentlen;
// open a file stream (system. io.
filestream FS = fileinf. openread ();
try
{< br> // write the uploaded file to the stream
// stream STRM = reqftp. getrequeststream ();
stream STRM = reqftp. getrequeststream ();
// 2kb of each file stream read
contentlen = FS. read (buff, 0, bufflength);
// The stream content has not ended
While (contentlen! = 0)
{
// Write content from file stream to upload stream
STRM. Write (buff, 0, contentlen );
Contentlen = FS. Read (buff, 0, bufflength );
}
// close two streams
STRM. close ();
FS. close ();
}< br> catch (exception ex)
{< br> throw ex;
}< BR >}