如何在 C# 中将字符串数据作为 ZIP 存档上传到 FTP 服务器
How to upload a string data as a ZIP archive to an FTP server in C#
这是我的代码。我想 export/upload 这个 .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!";
}
据我了解,您想将 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!";
}
}
如果您想压缩内存中的字符串 (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());
基于Zip a directory and upload to FTP server without saving the .zip file locally in C#。
您关于相反操作的相关问题:
这是我的代码。我想 export/upload 这个 .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!";
}
据我了解,您想将 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!";
}
}
如果您想压缩内存中的字符串 (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());
基于Zip a directory and upload to FTP server without saving the .zip file locally in C#。
您关于相反操作的相关问题: