如何在c#中将字符串数据作为ZIP归档文件上传到FTP服务器



这是我的代码。我想将这个.dat文件导出/上传为zip格式到FTP服务器。我试了很多,但没有找到任何解决方案。谁都可以帮我解决这个问题。

public string ExportVoid(FileSetups fileSetup, HttpPostedFileBase file)
{
var sb = new System.Text.StringBuilder();
var list = _context.VOIDS.ToList();

foreach (var item in list)
{
sb.AppendFormat("{0}t{1}t{2}t{3}t{4}t{5}t{6}t{7}t{8}r", item.Date, item.Time, item.Shift, item.EmployeeID, item.Amount, item.Items, item.DrawerOpen, item.Postpone, item.LocationID);
}
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

WebClient myWebClient = new WebClient();
var dbftp = _context.FileSetup.SingleOrDefault();
var a = dbftp.FTP;
var v = Session["ClientId"];
var d = DateTime.Now.ToString("MM_dd_yyyy_hh:mm:ss");
string uriString = "ftp://MyFTP.com/Files/" + "Void" + ".dat";
myWebClient.Credentials = new NetworkCredential("userName", "password");
//Console.WriteLine("nPlease enter the data to be posted to the URI {0}:", uriString);
string postData = sb.ToString();
// Apply ASCII Encoding to obtain the string as a byte array.
byte[] postArray = Encoding.ASCII.GetBytes(postData);
myWebClient.Headers.Add("Content-Disposition", "attachment; filename=" + "Void.dat");

byte[] responseArray = myWebClient.UploadData(uriString, postArray);
return "Export Successfully!";
}

如果您想压缩内存中的字符串(postData)并将压缩文件上传到FTP服务器,您可以使用:

using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
// Repeat this block, if you want to add more files
ZipArchiveEntry entry = archive.CreateEntry("void.dat");
using (Stream entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream, Encoding.UTF8))
{
writer.Write(postData);
}
}
memoryStream.Seek(0, SeekOrigin.Begin);
var request = WebRequest.Create("ftp://ftp.example.com/remote/path/archive.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream ftpStream = request.GetRequestStream())
{
memoryStream.CopyTo(ftpStream);
}
}

如果文本很小,那么内存占用无关紧要,您可以将上传代码(从memoryStream.Seek到末尾的所有内容)简化为:

var client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadData(
"ftp://ftp.example.com/remote/path/archive.zip", memoryStream.ToArray());

基于压缩一个目录并上传到FTP服务器,而不用在c#中保存。Zip文件。


您的相关问题是关于一个相反的操作:
如何将数据从存储在FTP服务器上的ZIP文件导入到c#中的数据库

据我所知,您希望将StringBuilder的内容保存到.dat文件中。然后将。dat文件压缩为。zip文件。最后,您要将.zip文件发送到FTP服务器。

试试这样写:

public string ExportVoid(FileSetups fileSetup, HttpPostedFileBase file)
{
var sb = new System.Text.StringBuilder();
var list = _context.VOIDS.ToList();
foreach (var item in list)
{
sb.AppendFormat("{0}t{1}t{2}t{3}t{4}t{5}t{6}t{7}t{8}r", item.Date, item.Time, item.Shift, item.EmployeeID, item.Amount, item.Items, item.DrawerOpen, item.Postpone, item.LocationID);
}
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
string zipFileName = "my_file.zip";
string datFileName = "my_file.dat";
string destinationFolder = @"c:destinationFolder";
string sourceFolder = @"c:sourceFolder";
string ftpDestinationPath = "ftp://1.2.3.4/my_file.zip";
//Create .dat file with the StringBuilder content.
File.WriteAllText(Path.Combine(sourceFolder, datFileName), sb.ToString());
//Create .zip file containing the .dat file
ZipFile.CreateFromDirectory(sourceFolder, Path.Combine(destinationFolder, zipFileName));
//Send file to FTP server
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDestinationPath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("userName", "password");
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(Path.Combine(destinationFolder, zipFileName)))
{
fileContents = File.ReadAllBytes(Path.Combine(destinationFolder, zipFileName));
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse ftpResponse = (FtpWebResponse)request.GetResponse()
{
return "Export Successfully!";
}
}

相关内容

  • 没有找到相关文章

最新更新