gpt4 book ai didi

c# - 使用c#将文件上传到服务器

转载 作者:行者123 更新时间:2023-11-29 12:23:12 24 4
gpt4 key购买 nike

我正在尝试将文件上传到我的服务器,之后我会将路径保存在我的数据库中,但文件上传不起作用,这并不异常(exception),但当我检查我的文件夹时,我不这样做看不到任何上传的文件 这是我的代码:

private void bBrowse_Click(object sender, EventArgs e)
{
long size = -1;
string path = "";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{

openFileDialog1.Title = "Attach speech assessment document";
openFileDialog1.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
openFileDialog1.InitialDirectory = @"C:\";
fileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
path = Path.GetDirectoryName(openFileDialog1.FileName);
labelFileName.Text = path + "/" + fileName;


}
Console.WriteLine(path); // <-- Shows file path in debugging mode.

Console.WriteLine(result); // <-- For debugging use.
}

private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();

NetworkCredential nc = new NetworkCredential("username", "password");

Uri addy = new Uri(@"http://url/public_html/assessment" + fileName);

client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, labelFileName.Text);
MessageBox.Show(arrReturn.ToString());

}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}

最佳答案

多亏了@William的评论,我解决了这个问题,我忘了添加斜杠,而且我还在网址中写了http而不是ftp,这是主要错误!

编辑后的代码:

private void bBrowse_Click(object sender, EventArgs e)
{
long size = -1;
string path = "";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{

openFileDialog1.Title = "Attach speech assessment document";
openFileDialog1.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
openFileDialog1.InitialDirectory = @"C:\";
fileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
path = Path.GetDirectoryName(openFileDialog1.FileName);
labelFileName.Text = path + "/" + fileName;


}
Console.WriteLine(path); // <-- Shows file path in debugging mode.

Console.WriteLine(result); // <-- For debugging use.
}

private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();

NetworkCredential nc = new NetworkCredential("username", "password");

Uri addy = new Uri(@"ftp://url/public_html/assessment/" + fileName);

client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, labelFileName.Text);
MessageBox.Show(arrReturn.ToString());

}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}

关于c# - 使用c#将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com