Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

How to upload files from windows application to another localhost application

Hi,

I have a new requirement. I already working in webapplication on IIS. I want to create on windows application for loading data(like pdf files, document files) into that existing LocalHost.

I want this scenario from another windows application only.

Please hlep me, urgent requirement.

Thanx.
0
BDPL
Asked:
BDPL
1 Solution
 
rajeeshmcaCommented:
0
 
BDPLAuthor Commented:
HI,
I need to catch files from windows application, and need to upload the localhost directory, http://localhost.
please help me to achieve the task.

thanx.
0
 
namazikhahCommented:
hi
you should create an app that copy file to physical address of your host, you should know your host physical address first and after that by using System.IO namespace and using file class copy whatever you want into host directory

0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
amar31282Commented:
you can use webclient also in windows applications to upload files to ftp
0
 
BDPLAuthor Commented:
Hi,

I am still gettting problem to achieve the task.

please help me.

thanx
0
 
amar31282Commented:
well usually it doesn't happen that you get everthing bundled .. but you r lucky who is getting it...


Make an ftp system in you local system IIS.. and then use the below given code



in app.config add the following keys

  <appSettings>
    <add key="FTPServer" value="ftp://youcomputernameorftpsharenametoaccess"/>
    <add key="FTPUser" value="test1234"/>
    <add key="FTPPassword" value="test1234"></add>
  </appSettings>

Add Ref

using System.IO;
using System.Net;
using System.Configuration;



  public partial class UploadFile : Form
    {
        public UploadFile()
        {
            InitializeComponent();
        }
        public static int currentprogress = 0;
        public static long size;
        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFileToServer(lblFileName.Text, openFileDialog1.FileName);
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string FileName = "";
            btnChooseFile.Visible = false;
            FileName = openFileDialog1.FileName.Split('\\')[openFileDialog1.FileName.Split('\\').Length - 1];
            lblFileName.Text = FileName;
            btnUpload.Visible = true;
            btnReset.Visible = true;

        }
        #region UploadFile
        private void UploadFileToServer(string FileName,string FilePath)
        {

            DialogResult dr;
            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["FTPServer"]) || string.IsNullOrEmpty(ConfigurationManager.AppSettings["FTPUser"]) || string.IsNullOrEmpty(ConfigurationManager.AppSettings["FTPPassword"]))
            {
                dr = MessageBox.Show("FTP Settings are not complete.\nDo you want to change FTP Settings now?", "Error", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Yes)
                {
                    this.Close();
                    Settings frmSettings = new Settings();
                    if (Application.OpenForms["Settings"] != null)
                    {
                        frmSettings = (Settings)Application.OpenForms["Settings"];
                        frmSettings.Show();
                    }
                    else
                    {

                        // Make it a child of this MDI form before showing it.
                        frmSettings.MdiParent = AdvantaRapidFU.ActiveForm;
                        frmSettings.WindowState = FormWindowState.Maximized;
                        frmSettings.Show();
                    }

                }
                else
                {
                    this.Close();
                }
            }
            else
            {

                FileStream fs = new FileStream(FilePath, FileMode.Open);
                size = fs.Length;
                fs.Close();
                btnUpload.Visible = false;
                lblFileName.Visible = false;
                progressBar1.Visible = true;
                lblProgress.Visible = true;
                lblProgress2.Visible = true;
                lblProgress.Text = "Uploading..";
                lblProgress2.Text = "sent 0 bytes of " + Convert.ToString(size) + " bytes";
                btnReset.Visible = false;

                WebClient wc = new WebClient();
                wc.Credentials = new NetworkCredential(Convert.ToString(ConfigurationManager.AppSettings["FTPUser"]), Convert.ToString(ConfigurationManager.AppSettings["FTPPassword"]));
                string FTPAbsolutePath = Convert.ToString(ConfigurationManager.AppSettings["FTPServer"]);

                wc.UploadFileAsync(new Uri(FTPAbsolutePath + FileName), FilePath);
                wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
                wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
            }
        }

        void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {

            lblProgress.Text = "Uploaded 100%";
            lblProgress2.Text = "sent bytes " + Convert.ToString(size) + " of " + size;
            btnOK.Visible = true;

        }

        void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
           
            UpdateProgressBar(e.ProgressPercentage,e.BytesSent);

        }
        private void UpdateProgressBar(int p,long Bytes)
        {
                for (int i = currentprogress; i < p; i++)
                {
                    System.Threading.Thread.Sleep(10);
                    // progressBar1.Increment(p);
                    progressBar1.PerformStep();
                    progressBar1.PerformStep();
                }
            currentprogress = p;
            if (p < 50)
            {
                lblProgress2.Text = "sent " + Convert.ToString(Bytes) + " bytes of " + Convert.ToString(size);
                lblProgress.Text = "Uploading " + progressBar1.Value + "%";
            }
            else
            {
                lblProgress.Text = "Uploading 99%";
                lblProgress2.Text = "   Just a few bytes left";
            }
        }

        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            btnUpload.Visible = false;
            btnChooseFile.Visible = true;
            lblFileName.Text = "No File Choosen";
            openFileDialog1.Reset();
            lblFileName.Visible = true;
            btnOK.Visible = false;
            progressBar1.Visible = false;
            lblProgress.Visible = false;
            lblProgress2.Visible = false;
            progressBar1.Value = 0;
           
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            btnUpload.Visible = false;
            btnChooseFile.Visible = true;
            lblFileName.Text = "No File Choosen";
            openFileDialog1.Reset();
            lblFileName.Visible = true;
            btnOK.Visible = false;
            progressBar1.Visible = false;
            lblProgress.Visible = false;
            lblProgress2.Visible = false;
            progressBar1.Value = 0;
            btnReset.Visible = false;
        }
       
    }

Open in new window

0
 
BDPLAuthor Commented:
I have completed my work, through this coding hlep.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now