Estou na luta aqui nesse projeto com asp.net
, mais as classe estão certas como peguei em um tutorial aqui na net, elas não dão erro nenhum só quando executa o projeto ai mostra.
Estou na luta aqui nesse projeto com asp
, mais as classe estão certas como peguei em um tutorial aqui na net, elas não dão erro nenhum só quando executa o projeto ai mostra.
Estou na luta aqui nesse projeto com asp.net
, mais as classe estão certas como peguei em um tutorial aqui na net, elas não dão erro nenhum só quando executa o projeto ai mostra.
Então pessoal estouEstou na luta aqui nesse projeto com aspasp
, mais as classe estaoestão certas como peguei em um tutorial aqui na net, elas naonão dão erro nenhum sosó quando executa o projeto ai mostra .
Erro CS0103 O nome "gvFiles" não existe no contexto atual
tenho a class CS.aspx.cs
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
using System.IO; using System.Net; using System.Data;
public partial class CS : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
Erro CS0103 O nome "gvFiles" não existe no contexto atual
e tenho a outra classe chamada CS.aspxclass CS.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Data;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
using System.IO; using System.Net; using System.Data;
public partial class CSe tenho a outra classe chamada : System.Web.UI.Page
{CS.aspx
protectedusing voidSystem;
using Page_Load(objectSystem.Collections.Generic;
using sender,System.Linq;
using EventArgsSystem.Web;
using e)System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Data;
public partial class CS : System.Web.UI.Page
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
tryprotected void Page_Load(object sender, EventArgs e)
{
//Create FTP RequestServer URL.
FtpWebRequest request =string (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods"ftp://10.Ftp0.ListDirectoryDetails;0.101:2323";
//Enter FTP Server credentials.
//Folder requestname.Credentials = newLeave NetworkCredential("Username",blank "Password");
if you want to list files from root requestfolder.UsePassive = true;
request.UseBinary = true;
string request.EnableSslftpFolder = false;"Uploads/";
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))try
{
//Read the Response as String and split using NewCreate LineFTP characterRequest.
entriesFtpWebRequest request = reader.ReadToEnd(FtpWebRequest)WebRequest.SplitCreate(new string[] { Environment.NewLineftp },+ StringSplitOptions.RemoveEmptyEntries).ToList(ftpFolder);
}
request.Method = responseWebRequestMethods.Close();Ftp.ListDirectoryDetails;
//Create a DataTable.
DataTable dtFiles = new DataTable();
//Enter FTP Server dtFiles.Columnscredentials.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
// request.Credentials = new DataColumnNetworkCredential("Size""Username", typeof(decimal)"Password"),;
request.UsePassive = true;
request.UseBinary = true;
newrequest.EnableSsl DataColumn("Date",= typeof(string))});false;
//LoopFetch the Response and addread detailsit ofusing eachStreamReader.
File to the DataTable FtpWebResponse response = (FtpWebResponse)request.GetResponse();
foreach (string entry in List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string[] splits //Read the Response as String and split using New Line character.
entries = entryreader.ReadToEnd().Split(new string[] { " ",Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//DetermineCreate whethera entryDataTable.
is for File or Directory.
DataTable dtFiles = new DataTable();
bool isFile = splits[0] dtFiles.SubstringColumns.AddRange(0new DataColumn[3] { new DataColumn("Name", 1typeof(string)),
!= "d";
bool isDirectory = splits[0].Substring new DataColumn(0"Size", 1typeof(decimal)),
== "d"; new DataColumn("Date", typeof(string))});
//IfLoop entryand isadd fordetails File,of addeach detailsFile to the DataTable.
ifforeach (isFilestring entry in entries)
{
dtFiles.Rowsstring[] splits = entry.AddSplit(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
dtFiles.Rows[dtFiles.Rows.Count//Determine -whether 1]["Size"]entry =is decimal.Parse(splits[4])for /File 1024;
or Directory.
dtFiles.Rows[dtFiles.Rows.Count -bool 1]["Date"]isFile = stringsplits[0].JoinSubstring(" "0, splits[5],1) splits[6],!= splits[7]);"d";
stringbool nameisDirectory = stringsplits[0].Empty;Substring(0, 1) == "d";
for//If (intentry iis =for 8;File, iadd <details splitsto DataTable.Length;
i++ if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
//Download the File.
throw new response.GetResponseStreamException().CopyTo(stream);
Responseex.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
as Response.BinaryWrite(stream.ToArray()FtpWebResponse);
Response.End(StatusDescription);
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
em uma classe tenho o ID com o nome que esta dando o erro gvFilesgvFiles
, e a outra chama esse ID para montar o GridViewGridView
.
Então pessoal estou na luta aqui nesse projeto com asp, mais as classe estao certas como peguei em um tutorial aqui na net, elas nao dão erro nenhum so quando executa o projeto ai mostra .
Erro CS0103 O nome "gvFiles" não existe no contexto atual
tenho a class CS.aspx.cs
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
using System.IO; using System.Net; using System.Data;
public partial class CS : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
e tenho a outra classe chamada CS.aspx
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
using System.IO; using System.Net; using System.Data;
public partial class CS : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
em uma classe tenho o ID com o nome que esta dando o erro gvFiles, e a outra chama esse ID para montar o GridView.
Estou na luta aqui nesse projeto com asp
, mais as classe estão certas como peguei em um tutorial aqui na net, elas não dão erro nenhum só quando executa o projeto ai mostra.
Erro CS0103 O nome "gvFiles" não existe no contexto atual
tenho a class CS.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Data;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
e tenho a outra classe chamada CS.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Data;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to list files from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it using StreamReader.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
List<string> entries = new List<string>();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//Read the Response as String and split using New Line character.
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
response.Close();
//Create a DataTable.
DataTable dtFiles = new DataTable();
dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(decimal)),
new DataColumn("Date", typeof(string))});
//Loop and add details of each File to the DataTable.
foreach (string entry in entries)
{
string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
//Determine whether entry is for File or Directory.
bool isFile = splits[0].Substring(0, 1) != "d";
bool isDirectory = splits[0].Substring(0, 1) == "d";
//If entry is for File, add details to DataTable.
if (isFile)
{
dtFiles.Rows.Add();
dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
string name = string.Empty;
for (int i = 8; i < splits.Length; i++)
{
name = string.Join(" ", name, splits[i]);
}
dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
}
}
//Bind the GridView.
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string fileName = (sender as LinkButton).CommandArgument;
//FTP Server URL.
string ftp = "ftp://10.0.0.101:2323";
//FTP Folder name. Leave blank if you want to Download file from root folder.
string ftpFolder = "Uploads/";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
// request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
}
em uma classe tenho o ID com o nome que esta dando o erro gvFiles
, e a outra chama esse ID para montar o GridView
.