Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading files to file server using webclient class

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?

like image 540
JustLogic Avatar asked Nov 04 '08 21:11

JustLogic


2 Answers

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.

like image 169
TheSoftwareJedi Avatar answered Sep 18 '22 13:09

TheSoftwareJedi


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);
        }
    }
}
}
like image 32
2 revs, 2 users 96% Avatar answered Sep 19 '22 13:09

2 revs, 2 users 96%