c#如何上传大文件到ftp站点
本文关键字:文件 ftp 站点 | 更新日期: 2023-09-27 18:09:42
我正在开发一个windows应用程序备份(文件和sql server数据库)。现在我需要把这些文件(.rar文件)上传到我的ftp站点。对于上传,我使用以下代码:
string file = "D:''RP-3160-driver.zip";
//opening the file for read.
string uploadFileName = "", uploadUrl = "";
uploadFileName = new FileInfo(file).Name;
uploadUrl = "ftp://ftp.Sitename.com/tempFiles/";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
try
{
long FileSize = new FileInfo(file).Length; // File size of file being uploaded.
Byte[] buffer = new Byte[FileSize];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
fs = null;
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential("usernam", "password");
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
requestObj = null;
MessageBox.Show("File upload/transfer Successed.", "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
MessageBox.Show("File upload/transfer Failed.'r'nError Message:'r'n" + ex.Message, "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
string file = "D:''RP-3160-driver.zip";
//opening the file for read.
string uploadFileName = "", uploadUrl = "";
uploadFileName = new FileInfo(file).Name;
uploadUrl = "ftp://ftp.Sitename.com/tempFiles/";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
try
{
long FileSize = new FileInfo(file).Length; // File size of file being uploaded.
Byte[] buffer = new Byte[FileSize];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
fs = null;
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential("usernam", "password");
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
requestObj = null;
MessageBox.Show("File upload/transfer Successed.", "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
MessageBox.Show("File upload/transfer Failed.'r'nError Message:'r'n" + ex.Message, "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
此代码只上传大小为<5 Mb。但是我需要上传大于500Mb到1Gb的文件。有谁能帮我吗?
对于较大的文件,您可以选择读取文件流并在读取时将其写入输出流。
FileStream fs = null;
Stream rs = null;
try
{
string file = "D:''RP-3160-driver.zip";
string uploadFileName = new FileInfo(file).Name;
string uploadUrl = "ftp://ftp.Sitename.com/tempFiles/";
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential("usernam", "password");
rs = requestObj.GetRequestStream();
byte[] buffer = new byte[8092];
int read = 0;
while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, read);
}
rs.Flush();
}
catch (Exception exception)
{
MessageBox.Show("File upload/transfer Failed.'r'nError Message:'r'n" + exception.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (rs != null)
{
rs.Close();
rs.Dispose();
}
}
Stream
类有一个很好的方法CopyTo
你不需要从/到流读写。只用fs.CopyTo(requestStream);
使用此方法,您不必声明像new Byte[FileSize];