使用 webclient 类将文件上传到文件服务器 [英] Uploading files to file server using webclient class

查看:23
本文介绍了使用 webclient 类将文件上传到文件服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我试图在 C# .NET 中使用 webclient 类.

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

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

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.

对这个错误有什么想法吗?可以这样直接传输文件吗?使用 webclient 类还是其他类?

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

推荐答案

就用

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

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

A windows fileshare exposed via a UNC path is treated as part of the file system, and has nothing to do with the web.

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

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.

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

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!

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

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.

这篇关于使用 webclient 类将文件上传到文件服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆