+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    Newbie aspkiddy is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

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


    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);
    }
    What do you think of my integration code that you see above? is that this can work or I'm completely wrong


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

    [(using Systemafter :]

    Code:
    using ftplib;

  2. #2
    Programming Professional gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565's Avatar
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    255

    Re: Another method safer than StreamWriter and how can I save the file on another ser

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

  3. #3
    Newbie aspkiddy is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

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

  4. #4
    Programming Professional gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565's Avatar
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    255

    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)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts