asp.net通过FTP发送文件

时间:2017-12-21 17:48:43

标签: c# asp.net ftp ftpwebrequest

我使用DataTable创建一个CSV文件。然后,我正在尝试将其发送到FTP服务器。

DataTable dt = new DataTable();

/*
    Here I create my DataTable
*/

// Convert the file to .CSV
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

// Send the Property File (FTP)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + "myFile.csv");
request.Method = WebRequestMethods.Ftp.UploadFile;
byte[] fileBytes = Encoding.Default.GetBytes(sb.ToString());
//Enter FTP Server credentials
request.Credentials = new NetworkCredential("UserName", "Password");
request.ContentLength = fileBytes.Length; 
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(fileBytes, 0, fileBytes.Length);
    requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();

但是,它返回错误:The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."} System.Exception {System.Net.WebException}

我不明白。该文件将无法找到,因为我正在创建该文件,我不是从服务器上读取的。

有人知道我做错了吗?

由于

0 个答案:

没有答案