ASP.NET 中的多个文件上传
Multiple File Upload in ASP.NET
我正在创建一个 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));
}
}
}
现在,每当我点击“上传”时,我都可以连接到服务器,但它不会传输文件,错误非常简单:
Could not find file 'C:\Users\me\documents\visual studio 2010\Projects\projectname\project\bin\Debug\openFileDialog1'.
但这部分我过不去。如何从列表框中引用我想要的文件路径?
这是我的“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();
}
感觉实现起来很简单,就是看不出来
我做错了什么?
当您填充列表框时,您将删除路径,只包含文件名。
当您上传时,您只使用文件名而缺少路径,因此程序会在默认目录中查找。
要验证,请将文件名的完整路径留在列表框中,看看它是否有效。
要修复,如果您不想在列表框中显示它们,则必须将完整路径保存在某个地方。
添加到史蒂夫的回答中:
我创建了一个列表 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;
因为我打算发送大量文件,如果我保持连接那么长时间它会在一段时间后断开,所以我想更明智的做法是为每个上传的文件打开和关闭请求。
我正在创建一个 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));
}
}
}
现在,每当我点击“上传”时,我都可以连接到服务器,但它不会传输文件,错误非常简单:
Could not find file 'C:\Users\me\documents\visual studio 2010\Projects\projectname\project\bin\Debug\openFileDialog1'.
但这部分我过不去。如何从列表框中引用我想要的文件路径? 这是我的“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();
}
感觉实现起来很简单,就是看不出来
我做错了什么?
当您填充列表框时,您将删除路径,只包含文件名。
当您上传时,您只使用文件名而缺少路径,因此程序会在默认目录中查找。
要验证,请将文件名的完整路径留在列表框中,看看它是否有效。
要修复,如果您不想在列表框中显示它们,则必须将完整路径保存在某个地方。
添加到史蒂夫的回答中:
我创建了一个列表 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;
因为我打算发送大量文件,如果我保持连接那么长时间它会在一段时间后断开,所以我想更明智的做法是为每个上传的文件打开和关闭请求。