我有一个3 gig XML文件,可以在c#中使用FTP上传
我这样做的方式不起作用
我收到错误,说流已关闭......
和一个0字节的文件当我做一个较小的flie它工作...
我需要帮助:))
m_reset.Reset();
FtpClient conn = new FtpClient();
_LocalPath = LocalFilePath;
conn.Host = record.Attribute("ftpServer").Value.ToString();
conn.Credentials = new NetworkCredential(record.Attribute("ftpUser").Value.ToString(), record.Attribute("ftpPass").Value.ToString());
string fname = DateTime.Now.ToString("EnvisionPush-{0:yyyy-MM-dd_hh_mm_ss_tt}"+".xml");
conn.BeginOpenWrite(fname, new AsyncCallback(BeginOpenWriteCallback), conn);
m_reset.WaitOne();
conn.Disconnect();
static void BeginOpenWriteCallback(IAsyncResult ar) {
FtpClient conn = ar.AsyncState as FtpClient;
Stream istream = null, ostream = null;
byte[] buf = new byte[8192];
int read = 0;
try {
if (conn == null)
throw new InvalidOperationException("The FtpControlConnection object is null!");
ostream = conn.EndOpenWrite(ar);
istream = new FileStream(_LocalPath, FileMode.Open, FileAccess.Read);
while ((read = istream.Read(buf, 0, buf.Length)) > 0) {
ostream.Write(buf, 0, read);
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
try {
if (istream != null)
istream.Close();
if (ostream != null)
ostream.Close();
m_reset.Set();
}
catch (Exception e) {
}
}
}
答案 0 :(得分:0)
使用此sample:
/// <summary>
/// Methods to upload file to FTP Server
/// </summary>
/// <param name="_FileName">local source file name</param>
/// <param name="_UploadPath">Upload FTP path including Host name</param>
/// <param name="_FTPUser">FTP login username</param>
/// <param name="_FTPPass">FTP login password</param>
public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
{
System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);
// Create FtpWebRequest object from the Uri provided
System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));
// Provide the WebPermission Credintials
_FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
_FtpWebRequest.KeepAlive = false;
// set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000;
// Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
_FtpWebRequest.UseBinary = true;
// Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
System.IO.FileStream _FileStream = _FileInfo.OpenRead();
try
{
// Stream to which the file to be upload is written
System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
// Read from the file stream 2kb at a time
int contentLen = _FileStream.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen);
contentLen = _FileStream.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
_Stream.Close();
_Stream.Dispose();
_FileStream.Close();
_FileStream.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}