使用 webclient 类将文件上传到文件服务器

发布于 2024-07-08 13:05:58 字数 798 浏览 8 评论 0原文

目前,我有一个应用程序可以从我的网络应用程序接收上传的文件。 我现在需要将该文件传输到恰好位于同一网络上的文件服务器(但情况可能并不总是如此)。

我试图在 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);
    }

位于 192.168.1.28 的机器是文件服务器,具有共享 c:\Files。 截至目前,我收到登录失败错误用户名或密码的错误,但我可以打开资源管理器并输入该路径成功登录。 我还可以使用远程桌面登录,因此我知道用户帐户有效。

关于这个错误有什么想法吗? 可以直接这样传输文件吗? 使用网络客户端类或者其他类?

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).

I was attempting to use the webclient class in C# .NET.

    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.

Any ideas on this error?
Is it possible to transfer a file directly like that? With the webclient class or maybe some other class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-07-15 13:05:58

只需使用

File.Copy(filepath, "\\\\192.168.1.28\\Files");

通过 UNC 路径公开的 Windows 文件共享被视为文件系统的一部分,与 Web 无关。

使用的凭据将是 ASP.NET 工作进程的凭据,或者您已启用的任何模拟。 如果您可以调整这些以使其正确,那么这是可以完成的。

您可能会遇到问题,因为您使用的是 IP 地址而不是服务器名称(Windows 信任设置阻止离开域 - 通过使用 IP,您将隐藏任何域详细信息)。 如果可能,请使用服务器名称!

如果这不在同一个 Windows 域上,并且您尝试使用不同的域帐户,则需要将用户名指定为“[domain_or_machine] \[username]"

如果您需要指定显式凭据,则需要查看 编码模拟解决方案

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.

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.

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!

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]"

If you need to specify explicit credentials, you'll need to look into coding an impersonation solution.

记忆消瘦 2024-07-15 13:05:58
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);
        }
    }
}
}
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);
        }
    }
}
}
素罗衫 2024-07-15 13:05:58

当您手动打开 IP 地址(通过 RUN 命令或映射网络驱动器)时,您的 PC 将通过管道发送您的凭据,文件服务器将接收来自 DC 的授权。

当 ASP.Net 尝试时,它将尝试使用 IIS 工作用户(除非打开模拟,这会列出一些其他问题)。 传统上,IIS 工作用户无权跨服务器(甚至在 Web 服务器上的其他文件夹中)工作。

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文