1

Using the link below, I wrote a code for my application. I am not able to get it right though, Please refer the link and help me ot with it...

Uploading files to file server using webclient class


The following is my code:-

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

}

I also used:- File.Copy(filePath, "\\192.168.1.3\upload\");

The following line doesn't execute...

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

tried changing it to:-

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

IT still doesn't work...Any solution to it??

I basically want to transfer a file from the client to the file storage server without actually login into the server so that the client cannot access the storage location on the server directly.

THIS IS THE ERROR I GET :- "An exception occurred during a WebClient request"

Community
  • 1
  • 1
Imcl
  • 11
  • 1
  • 2
  • 7
  • Do you have an exception, or it just does nothing? If the store is secure (i.e. needs a user/pass) that will need to be provided either by the user or by impersonation before your code would work regardless. – GrayWizardx Mar 15 '10 at 07:20
  • Yesm we really need more. "Does not work" means nothing. What happens? I would sort of think... a security exception that actually explains the error ;) – TomTom Mar 15 '10 at 07:23
  • byte[] arrReturn = client.UploadFile(addy, filePath); The Code Skips the Above Line... – Imcl Mar 15 '10 at 08:47
  • THIS IS THE ERROR I GET :- "An exception occurred during a WebClient request" – Imcl Mar 17 '10 at 06:27
  • Why are you using a `WebClient` for a `File.Copy`? – leppie Mar 15 '10 at 07:30
  • I used this at first, didnt workout for me so tried the File.Copy.. byte[] arrReturn = client.UploadFile(addy, filePath); – Imcl Mar 15 '10 at 08:45

2 Answers2

1

Copying files from one folder to a different folder is very easy.
The below code is in C#.NET.
First add System.IO and System.NET to your namespace. Then add the following code.

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

    }


If you want to copy new files and REPLACE existing files just add 'true' to the file.copy . The full code is:

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

    }
bluwater2001
  • 7,829
  • 5
  • 24
  • 21
0

Please read the following MSDN article, as it may help you out answering your question.

EDIT: Here is a (possible) answer to what you're looking for... To copy files from local machine you can simply use System.IO.File.Copy(), as you're are already logged in to that machine. However, to copy the files from a remote machine where you have not (yet) logged in, you have to provide domainname, username and password to proceed with authentication on the remote machine. Please test this and confirm if it does what you want it to do :)

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

    }
}
XpiritO
  • 2,809
  • 5
  • 28
  • 34
  • I have already checked it out earlier. That code uploads the file on the client side itself. I need a code that will upload a file on storage server(another machine and not the user machine) in the shared folder. – Imcl Mar 15 '10 at 10:35
  • I actually want to copy/transfer files from the local machine to the remote machine. – Imcl Mar 15 '10 at 11:20
  • 1
    I think you provided the code for a Window-Based Application. Its a Web-Based Application that I am working on. – Imcl Mar 15 '10 at 11:23
  • But if what you want is to transfer a file from one machine to another, why do you have to do it in a web based application? Can't you execute this locally on that machine, on an external process? (not copulated to a web based component) – XpiritO Mar 15 '10 at 12:08
  • This is an intranet project and therefore its a web-based application. I need to transfer the files to the server machine so that multiple users can access them from their machines as and when required. – Imcl Mar 17 '10 at 05:53
  • THIS IS THE ERROR I GET :- "An exception occurred during a WebClient request" – Imcl Mar 17 '10 at 06:28