Home > Net >  Ftp upload a file error
Ftp upload a file error

Time:04-20

 public void UploadFile (string srcFileFullName) 
{
UploadFile (srcFileFullName, null);
}
///& lt; Summary>
///upload file
///& lt;/summary>
///& lt; Param name="the fileinfo & gt;" Need to upload the file & lt;/param>
///& lt; Param name="targetDir & gt;" The target path & lt;/param>
///& lt; Param name="hostname" & gt; The FTP address & lt;/param>
///& lt; Param name="username" & gt; The FTP user name & lt;/param>
///& lt; Param name="password" & gt; The FTP password & lt;/param>
///
Public void UploadFile (string srcFileFullName, string destNewName)
{
The FileInfo the FileInfo=new the FileInfo (srcFileFullName);
If (string. IsNullOrEmpty (destNewName))
{
DestNewName=the fileinfo. Name;
}

String uri=fURI. TrimEnd ('/') + "/" + destNewName;

FtpWebRequest FTP=null;

FTP=(FtpWebRequest) FtpWebRequest. Create (new Uri (Uri));
FTP. KeepAlive=true;
FTP. Credentials=new NetworkCredential (fUserID, fPassword);
FTP. Timeout=300000;

//set the FTP command
The FTP Method=System.Net.WebRequestMethods.Ftp.UploadFile;
FTP. UseBinary=true;
FTP. UsePassive=true;


//tell the FTP file size
FTP. ContentLength=the fileinfo. Length;

Const int BufferSize=2048;
Byte [] the content=new byte [BufferSize - 1 + 1];
Int dataRead;
//upload file content
Using (FileStream fs=the fileinfo. OpenRead ())
{
Try
{
Using (Stream rs=FTP. GetRequestStream ())
{
Do
{
DataRead=fs. Read (content, 0, BufferSize);
Rs. Write (content, 0, dataRead);
} while (! (dataRead & lt; BufferSize));
Rs. The Close ();
}

}
The catch (Exception ex)
{
Throw new Exception (ex. Message);
}
The finally
{
Fs. The Close ();
}

}
}


CodePudding user response:

This is the mistake, please help have a look at the great god, but these documents address is no problem, I upload again and succeeded, after

  •  Tags:  
  • C#
  • Related