Refer the below sample code how to generate Log in notepad file. I referred same Question code.
C#
protected void DownloadMultipleFileAsZip(object sender, EventArgs e)
{
List<Files> fileList = new List<Files>();
foreach (GridViewRow row in gvFiles.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
string fileName = row.Cells[1].Text.Trim();
string ftp = "ftp://yourserver.com/";
string ftpFolder = "Uploads/";
try
{
WebClient request = new WebClient();
string url = ftp + ftpFolder + fileName;
request.Credentials = new NetworkCredential("Username", "Password");
byte[] bytes = request.DownloadData(url);
fileList.Add(new Files() { FileName = fileName, Bytes = bytes });
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (Files file in fileList)
{
zip.AddEntry(file.FileName, file.Bytes);
}
LogFile(string.Format("Total files {0} and downloaded files {1}", gvFiles.Rows.Count.ToString(), fileList.Count));
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
private void LogFile(string filedetsils)
{
string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
message += string.Format("Message: {0}", filedetsils);
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
string path = Server.MapPath("~/Log/LogFile.txt");
if (!(File.Exists(path)))
{
FileStream oFs = new FileStream(path, FileMode.Create);
oFs.Close();
oFs.Dispose();
}
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(message);
writer.Close();
}
}
VB.Net
Protected Sub DownloadMultipleFileAsZip(ByVal sender As Object, ByVal e As EventArgs)
Dim fileList As List(Of Files) = New List(Of Files)()
For Each row As GridViewRow In gvFiles.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
Dim fileName As String = row.Cells(1).Text.Trim()
Dim ftp As String = "ftp://yourserver.com/"
Dim ftpFolder As String = "Uploads/"
Try
Dim request As WebClient = New WebClient()
Dim url As String = ftp & ftpFolder & fileName
request.Credentials = New NetworkCredential("Username", "Password")
Dim bytes As Byte() = request.DownloadData(url)
fileList.Add(New Files() With {.FileName = fileName, .Bytes = bytes})
Catch ex As WebException
Throw New Exception((TryCast(ex.Response, FtpWebResponse)).StatusDescription)
End Try
End If
Next
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName("Files")
For Each file As Files In fileList
zip.AddEntry(file.FileName, file.Bytes)
Next
LogFile(String.Format("Total files {0} and downloaded files {1}", gvFiles.Rows.Count.ToString(), fileList.Count))
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.[End]()
End Using
End Sub
Private Sub LogFile(ByVal filedetsils As String)
Dim message As String = String.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
message += Environment.NewLine
message += "-----------------------------------------------------------"
message += Environment.NewLine
message += String.Format("Message: {0}", filedetsils)
message += Environment.NewLine
message += "-----------------------------------------------------------"
message += Environment.NewLine
Dim path As String = Server.MapPath("~/Log/LogFile.txt")
If Not (File.Exists(path)) Then
Dim oFs As FileStream = New FileStream(path, FileMode.Create)
oFs.Close()
oFs.Dispose()
End If
Using writer As StreamWriter = New StreamWriter(path, True)
writer.WriteLine(message)
writer.Close()
End Using
End Sub
Screenshot
