【问题标题】:Multiple File Upload in ASP.NETASP.NET 中的多文件上传
【发布时间】:2015-09-17 22:08:35
【问题描述】:

我正在创建一个 FTP 客户端,它允许我将文件上传到从组合框中选择的确定服务器。我的代码适用于单个文件,但尝试将其调整为多个文件一直具有挑战性。我已经阅读了有关异步上传的内容,但我无法理解,因此我们将不胜感激。

上传方式:

private void upload(string filepath, string address, string username, string password)
    {
            //CONNECT
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address + "/" + Path.GetFileName(filepath));
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            MessageBox.Show(response.WelcomeMessage + " to " + comboBox1.Text + " SERVER");
            //PREPARE FILE
            FileStream stream = File.OpenRead(filepath);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            //UPLOAD FILE
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();
            MessageBox.Show("Upload complete on " + comboBox1.Text + " SERVER " + response.StatusDescription);
            
       }

我把要上传的文件放到一个ListBox中,代码如下:

 private void button2_Click(object sender, EventArgs e)
    {
        //THIS WAS FOR SINGLE FILE UPLOAD
        //if (openFileDialog1.ShowDialog() == DialogResult.OK)
        //textBox1.Text = openFileDialog1.FileName;

       //MULTIPLE FILE UPLOAD
        OpenFileDialog openFiles = new OpenFileDialog();
        openFiles.Filter = "PDF, XML, TXT Files(*.pdf;*.xml;*.txt)|*.pdf;*.xml;*.txt|All Files(*.*)|*.*";
        openFiles.Multiselect = true;
        //openFiles.InitialDirectory = "C:\\";
        //openFiles.RestoreDirectory = true;
        openFiles.Title = "Select Files";

        if (openFiles.ShowDialog() == DialogResult.OK)
        {
            foreach (string file in openFiles.FileNames)
            {
                listBox1.Items.Add(System.IO.Path.GetFileName(file));
            }
        }              
      
    }

现在,每当我点击“上传”时,我都可以连接到服务器,但它不会传输文件,错误非常直接:

找不到文件'C:\Users\me\documents\visual studio 2010\Projects\projectname\project\bin\Debug\openFileDialog1'。

但我无法通过这部分。如何从 ListBox 中引用我想要的文件路径? 这是我的“upload_click”代码,它只是我可以访问的 3 个服务器之一,但是一旦我让第一个工作正常,其他的应该很容易。

 private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == "server" && listBox1.Items.Count > 0)
        {
            while (listBox1.Items.Count > 0)
            {
                upload(openFileDialog1.FileName, "ftp://000.000.0.000", "username", "password");
                listBox1.Items.RemoveAt(0);
            }
            textBox1.Text = " ";
            comboBox1.Text = "Select Server...";
            pictureBox1.Image = null;
            //listBox1.Items.Clear();
        }

我觉得它只是一个简单的事情来实现,但我看不到它。

我做错了什么?

【问题讨论】:

    标签: c# file-upload ftp ftp-client ftpwebrequest


    【解决方案1】:

    当您填充列表框时,您将剥离路径并仅包含文件名。

    当您上传时,您只使用文件名并且缺少路径,因此程序会在默认目录中查找。

    要验证,请将文件名的完整路径留在列表框中,看看它是否有效。

    要解决此问题,如果您不希望它们显示在列表框中,则必须将完整路径保存在某处。

    【讨论】:

      【解决方案2】:

      补充史蒂夫的回答:

      我创建了一个列表List<string> fullFileName 来保存文件的完整路径。然后在打开OpenFileDialog 时,我将完整路径保存为

      fullFileName = new List<string>(openFiles.FileNames)

      然后对于每个选择的文件,我在listBox 中添加了文件名,并用listBox1.Items.Add(Path.GetFileNameWithoutExtension(file)) 剥离了路径

      然后对于 upload 方法参数,我只使用了我存储在列表中的路径

      fullFileName[listBox1.SelectedIndex]

      并将request.KeepAlive = false;更改为request.KeepAlive = true;,因为我打算发送大量文件,如果我让连接保持活动那么长时间它会在一段时间后死亡,所以我认为打开它更可取并关闭每个上传文件的请求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-12
        • 2019-01-29
        • 2016-08-10
        • 2011-10-12
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多