Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Another method safer than StreamWriter and how can I save the file on another server

streamwriter stream

  • Please log in to reply
3 replies to this topic

#1 aspkiddy

aspkiddy

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 15 October 2009 - 06:08 AM

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.

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

StreamWriter SW = new StreamWriter(@"\\111.222.1.00\c$\Inetpub\wwwroot\site_toto\ save\information\Data_" + strDate + ".csv");

It must disable the firewall on server so that it works. I can’t it.

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 ?:confused:


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);
}

What do you think of my integration code that you see above? is that this can work or I'm completely wrong:confused:


the beginning of the code should I add [using ftplib]

[(using System;)after :]

using ftplib;

  • 0

#2 gaylo565

gaylo565

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 258 posts

Posted 15 October 2009 - 08:26 AM

I don't know what you have their but it doesn't look like a ftp uploader to me? Here is an example of one that I have used before. It uses a form with a few text boxes and a couple of buttons. You dont have to use the form if you don't need it.
ex:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace FTPUploader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void uploadFile(string FTPAddress, string filePath, string username, string password)
        {
            //Create FTP request
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            //Load the file
            FileStream stream = File.OpenRead(filePath);
            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");
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            Application.DoEvents();

            uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text);
            btnUpload.Enabled = true;
        }

        private void txtFTPAddress_Leave(object sender, EventArgs e)
        {
            if (!txtFTPAddress.Text.StartsWith("ftp://"))
                txtFTPAddress.Text = "ftp://" + txtFTPAddress.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFile1.ShowDialog() == DialogResult.OK)
                txtFilePath.Text = openFile1.FileName;
        }
    }
}
I would have included the source code but I am kind of in a hurry and I need a new copy of winzip to archive before I can add it. Maybe I will post with the complete code for the app in a while. Hope this helps out
  • 0

#3 aspkiddy

aspkiddy

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 15 October 2009 - 10:32 AM

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 ? :

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");
        }

  • 0

#4 gaylo565

gaylo565

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 258 posts

Posted 16 October 2009 - 07:56 AM

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)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download