简体   繁体   中英

Select and upload a file to a specific folder

I need to upload one selected file to a specific folder. I have this code:

using System.IO;

namespace FTP_UPLOAD
{
    public partial class FTPUPLOAD : Form
    {
        public FTPUPLOAD()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "zip files (*.zip)|*.zip";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //Get the path of specified file
                    filePath = openFileDialog.FileName;

                    //Read the contents of the file into a stream
                    var fileStream = openFileDialog.OpenFile();
                    {
                    }
                }
            }
            MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
        }
    }
}

I needed to define where the file will be uploaded to (like c:\\ftp). After the process was completed, I wanted to show the user the full path to where that file is located, like (filename = file.zip) than: ftp.mysite/file.zip

要复制文件,可以使用:

File.Copy(openFileDialog.FileName, "c:\\ftp\" + openFileDialog.SafeFileName,true);

You need to use the SaveFileDialog class after you obtain the path of the file you want to save. Add using System.IO; to use the FileInfo class.

private void SaveFile(string filePath)
{
  FileInfo file = new FileInfo(filePath);

  using (SaveFileDialog saveFileDialog = new SaveFileDialog())
  {
    saveFileDialog.FileName = filePath;
    if( saveFileDialog.ShowDialog() == DialogResult.OK)
    {
      MessageBox.Show("Your file was saved at " + saveFileDialog.FileName);
    }                
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM