C# 使用 webclient 类将文件上传到文件服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/263518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading files to file server using webclient class
提问by JustLogic
Currently I have an application that receives an uploaded file from my web application. I now need to transfer that file to a file server which happens to be located on the same network (however this might not always be the case).
目前我有一个应用程序从我的 Web 应用程序接收上传的文件。我现在需要将该文件传输到恰好位于同一网络上的文件服务器(但情况并非总是如此)。
I was attempting to use the webclient class in C# .NET.
我试图在 C# .NET 中使用 webclient 类。
string filePath = "C:\test\564.flv";
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential(uName, password);
Uri addy = new Uri("\\192.168.1.28\Files\test.flv");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, filePath);
Console.WriteLine(arrReturn.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The machine located at 192.168.1.28 is a file server and has a share c:\Files. As of right now I am receiving an error of Login failed bad user name or password, but I can open explorer and type in that path login successfully. I can also login using remote desktop, so I know the user account works.
位于 192.168.1.28 的机器是文件服务器,共享 c:\Files。截至目前,我收到登录失败错误用户名或密码的错误,但我可以打开资源管理器并成功输入该路径登录。我也可以使用远程桌面登录,所以我知道用户帐户有效。
Any ideas on this error? Is it possible to transfer a file directly like that? With the webclient class or maybe some other class?
关于这个错误的任何想法?是否可以像这样直接传输文件?使用 webclient 类还是其他类?
采纳答案by TheSoftwareJedi
Just use
只需使用
File.Copy(filepath, "\\192.168.1.28\Files");
A windows fileshare exposed via a UNC path is treated as part of the file system, and has nothing to do with the web.
通过 UNC 路径公开的 Windows 文件共享被视为文件系统的一部分,与网络无关。
The credentials used will be that of the ASP.NET worker process, or any impersonation you've enabled. If you can tweak those to get it right, this can be done.
使用的凭据将是 ASP.NET 工作进程的凭据,或您启用的任何模拟。如果您可以调整它们以使其正确,则可以完成。
You may run into problems because you are using the IP address instead of the server name (windows trust settings prevent leaving the domain - by using IP you are hiding any domain details). If at all possible, use the server name!
您可能会遇到问题,因为您使用的是 IP 地址而不是服务器名称(Windows 信任设置会阻止离开域 - 通过使用 IP,您会隐藏任何域详细信息)。 如果可能,请使用服务器名称!
If this is not on the same windows domain, and you are trying to use a different domain account, you will need to specify the username as "[domain_or_machine]\[username]"
如果这不在同一个 Windows 域中,并且您尝试使用不同的域帐户,则需要将用户名指定为“[domain_or_machine]\[username]”
If you need to specify explicit credentials, you'll need to look into coding an impersonation solution.
如果需要指定显式凭据,则需要考虑编写模拟解决方案。
回答by Stephen Wrighton
when you manually open the IP address (via the RUN command or mapping a network drive), your PC will send your credentials over the pipe and the file server will receive authorization from the DC.
当您手动打开 IP 地址(通过 RUN 命令或映射网络驱动器)时,您的 PC 将通过管道发送您的凭据,文件服务器将收到来自 DC 的授权。
When ASP.Net tries, then it is going to try to use the IIS worker user (unless impersonation is turned on which will list a few other issues). Traditionally, the IIS worker user does not have authorization to work across servers (or even in other folders on the web server).
当 ASP.Net 尝试时,它将尝试使用 IIS 工作用户(除非打开模拟,这将列出一些其他问题)。传统上,IIS 工作用户无权跨服务器(甚至在 Web 服务器上的其他文件夹中)工作。
回答by Erandika Sandaruwan
namespace FileUpload
{
public partial class Form1 : Form
{
string fileName = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "";
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Attach customer proposal document";
fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
fDialog.InitialDirectory = @"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
fileName = System.IO.Path.GetFileName(fDialog.FileName);
path = Path.GetDirectoryName(fDialog.FileName);
textBox1.Text = path + "\" + fileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential("erandika1986", "123");
Uri addy = new Uri(@"\192.168.2.4\UploadDocs\"+fileName);
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, textBox1.Text);
MessageBox.Show(arrReturn.ToString());
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
}
}