简体   繁体   English

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

[英]Uploading files to file server using webclient class

Currently I have an application that receives an uploaded file from my web application. 目前我有一个应用程序从我的Web应用程序接收上传的文件。 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. 我试图在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. 位于192.168.1.28的计算机是文件服务器,共享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? 使用webclient类或者其他类?

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文件共享被视为文件系统的一部分,与Web无关。

The credentials used will be that of the ASP.NET worker process, or any impersonation you've enabled. 使用的凭据将是ASP.NET辅助进程的凭据,或者您启用的任何模拟。 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). 您可能会遇到问题,因为您使用的是IP地址而不是服务器名称(Windows信任设置会阻止离开域 - 通过使用IP隐藏任何域详细信息)。 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]" 如果这不在同一个Windows域中,并且您尝试使用其他域帐户,则需要将用户名指定为“[domain_or_machine] \\ [username]”

If you need to specify explicit credentials, you'll need to look into coding an impersonation solution . 如果您需要指定显式凭据,则需要查看对模拟解决方案的编码

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);
        }
    }
}
}

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). 当ASP.Net尝试时,它将尝试使用IIS工作者用户(除非启用模拟,这将列出一些其他问题)。 Traditionally, the IIS worker user does not have authorization to work across servers (or even in other folders on the web server). 传统上,IIS工作者用户无权跨服务器(甚至Web服务器上的其他文件夹)工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM