C# 在MVC/C中创建CSV文件并通过FTP上传到服务器#
我正在从C# 在MVC/C中创建CSV文件并通过FTP上传到服务器#,c#,asp.net-mvc,ftp,export-to-csv,C#,Asp.net Mvc,Ftp,Export To Csv,我正在从列表创建CSV文件,如下所示: public void ExportListToCSV() { StringWriter sw = new StringWriter(); sw.WriteLine("\"username\",firstname, lastname , email , course1 , course2 , course3 , course4 "); Response.ClearContent(); Response.AddHeader
列表创建CSV文件,如下所示:
public void ExportListToCSV()
{
StringWriter sw = new StringWriter();
sw.WriteLine("\"username\",firstname, lastname , email , course1 , course2 , course3 , course4 ");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=OgrenciListesi.csv");
Response.ContentType = "text/csv";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
Response.Charset = "windows-1254";
foreach (var line in Liste())
{
sw.WriteLine(string.Format("\"{0}\", {1} , {2} , {3} , {4} , {5} , {6} , {7} ",
line.KullaniciKodu,
line.Adi,
line.Soyadi,
line.Email,
line.Course1,
line.Course2,
line.Course3,
line.Course4
));
}
Response.Write(sw.ToString());
Response.End();
}
我想通过FTP以相同的方式(可能以相同的方式)将此CSV发送到服务器。如何处理此CSV并通过C#中的FTP上传到服务器?我以前使用过网络客户端
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
string address= @"ftp://example.com/";
string filename= @"C:\foo.csv"
client.UploadFile(address, filename);
您可以在这里上传到FTP服务器(摘自Microsoft网站)
我将StringWriter结果放入MemoryStream并将其发送到UploadFile方法,现在一切正常
...
try
{
var data = UnicodeEncoding.ASCII.GetBytes(sw.ToString());
using (Stream stream = new MemoryStream(data))
{
stream.Position = 0;
bool res = this.UploadFile(stream, "OgrenciListesi.csv", "tmp");
}
}
catch (Exception)
{
throw;
}
...
首先将其保存到文件中,而不是发送到客户端,然后使用FtpWebRequest将其上载到FTP服务器。看看这个,你是说保存到磁盘还是内存?(Diske kaydetemden yollamak istiyorum da):-)在这种情况下,尝试上传流而不保存到文件,但我的磁盘上没有这样的文件可发送。我想在内存中创建它并发送到FTP。
...
try
{
var data = UnicodeEncoding.ASCII.GetBytes(sw.ToString());
using (Stream stream = new MemoryStream(data))
{
stream.Position = 0;
bool res = this.UploadFile(stream, "OgrenciListesi.csv", "tmp");
}
}
catch (Exception)
{
throw;
}
...