将文件上载到文件服务器

时间:2010-03-15 07:15:58

标签: c# uri webclient byte

使用下面的链接,我为我的应用程序编写了一个代码。我无法做到正确,请参考链接并帮助我使用它......

Uploading files to file server using webclient class


以下是我的代码: -

protected void Button1_Click(object sender, EventArgs e)
{

    filePath = FileUpload1.FileName;    
    try
    {
        WebClient client = new WebClient();

        NetworkCredential nc = new NetworkCredential(uName, password);

        Uri addy = new Uri("\\\\192.168.1.3\\upload\\");
        client.Credentials = nc;
        byte[] arrReturn = client.UploadFile(addy, filePath);

        Console.WriteLine(arrReturn.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

}

我也用过: - File.Copy(filePath,“\\ 192.168.1.3 \ upload \”);

以下行不会执行...

byte[] arrReturn = client.UploadFile(addy, filePath);

尝试将其更改为: -

byte[] arrReturn = client.UploadFile("\\\\192.168.1.3\\upload\\", filePath);

IT仍然无效......任何解决方案?

我基本上想要将文件从客户端传输到文件存储服务器而不需要 实际登录到服务器,以便客户端无法访问存储位置 直接在服务器上。

这就是我得到的错误: - “WebClient请求期间发生异常”

2 个答案:

答案 0 :(得分:1)

将文件从一个文件夹复制到另一个文件夹非常简单。
以下代码在C#.NET中 首先将System.IO和System.NET添加到命名空间。然后添加以下代码。

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname);


        }
    }
    catch (Exception ex)
    {

    }


如果要复制新文件和REPLACE现有文件,只需在file.copy中添加“true”即可。完整代码是:

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname,true);


        }
    }
    catch (Exception ex)
    {

    }

答案 1 :(得分:0)

请阅读the following MSDN article,因为它可能会帮助您回答问题。

修改 以下是您正在寻找的(可能的)答案...... 要从本地计算机复制文件,您只需使用System.IO.File.Copy(),因为您已经登录到该计算机。但是,要从尚未登录的远程计算机复制文件,您必须提供domainnameusernamepassword以继续远程计算机上的身份验证。 请测试一下并确认它是否符合您的要求:)

public void copyRemoteFiles(string sourceFile, string destFile) {
    IntPtr admin_token = default(IntPtr);
    WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
    WindowsIdentity wid_admin = null;
    WindowsImpersonationContext wic = null;

    try {
        if (LogonUser(sUserName, sDomainName, sPassword, 9, 0, admin_token) != 0) {
            wid_admin = new WindowsIdentity(admin_token);
            wic = wid_admin.Impersonate();
            if (System.IO.File.Exists(sourceFile)) {
                System.IO.File.Copy(sourceFile, destFile, true);
            }
            else {
                //Copy Failed
                return;
            }
        }
        else {
            return;
        }
    }
    catch (System.Exception se) {
        int ret = Marshal.GetLastWin32Error();
        MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
        MessageBox.Show(se.Message);
        if (wic != null) {
            wic.Undo();
        }
        return;
    }
    finally {
        if (wic != null) {
            wic.Undo();
        }

    }
}