|
||||||
| C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Another method safer than StreamWriter and how can I save the file on another server
Another method safer than StreamWriter and how can I save the file on another server (asp.net c sharp)
I have a form: when the user fills a form, the application takes this information and creates a data file (.csv) and put it (data file) on the server in a directory (name of this directory is [information]) where the web site is located. Code:
using (System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("save/information/Data_" + strDate + ".csv")))
{
SW.WriteLine(s.FirstName + ";" + s.LastName + ";" + s.Address1 + ";");
SW.Close();
}
After, I changed the path for save the file, in a different server than the Web server Code:
StreamWriter SW = new StreamWriter(@"\\111.222.1.00\c$\Inetpub\wwwroot\site_toto\ save\information\Data_" + strDate + ".csv"); So instead of filing the file with system Windows, I'd like to put with FTP protocole on another place, but I do not know how I can do. I have a class (by J.P. Trosclair) ! How can I integrate it? Is that correct if I do like that ? ![]() Code:
using (FTP ftplib = new FTP(MapPath ("Data_" + strDate + ".csv")));
try
{
ftplib.Connect("ftp.toto.com",
"tata",
"pata");
ftplib.ChangeDir("information/");
}
catch(Exception ex)
{
Console.WriteLine(s.FirstName + ";" + s.LastName + ";" + s.Address1 + ";");
}
try
{
int perc = 0;
ftplib.OpenDownload("Data_" + strDate + ".csv", true);
while(ftplib.DoDownload() > 0)
{
perc = (int)((ftplib.BytesTotal * 100) / ftplib.FileSize);
Console.Write("\rDownloading: {0}/{1} {2}%",
ftplib.BytesTotal, ftplib.FileSize, perc);
Console.Out.Flush();
}
Console.WriteLine("");
}
catch(Exception ex)
{
Console.WriteLine("");
Console.WriteLine(ex.Message);
}
![]() the beginning of the code should I add [using ftplib] [(using System after :]Code:
using ftplib; |
|
|||
|
Re: Another method safer than StreamWriter and how can I save the file on another ser
Thanks Gaylo,
For exemple, if my ftp adrress is ftp.toto.com and My folder : ftp.toto.com/information/and my username : tataand my password:pata So I can change your code as like as ? : Code:
private void uploadFile(string FTPAddress, string filePath, string username, string password)
{
//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftp.toto.com + "/" + Path.GetFileName(information/));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(tata, pata);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Load the file
FileStream stream = File.OpenRead(information/);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//Upload file
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
MessageBox.Show("Uploaded Successfully");
}
|
|
||||
|
Re: Another method safer than StreamWriter and how can I save the file on another ser
The only problem I see with your code now is that you need your whole file name including the folder not just the folder name. Also make sure you change the parameter names in your method defenition so they match up with the ones you are using within your method. In mine it is set up to use a browse button so you can find the proper file through an explorer window and it just adds the file w/directory for me automatically. Also make sure you get rid of any extra /'s as you have whenever you use information. These will make it fail (if you just get rid of the forward slash's and change the names in the parameters to match the names you use in the method than the method should be fixed)
|
![]() |
| Tags |
| asp.net, c sharp, class, ftp |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 08:15 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc