简体   繁体   中英

Uploading files to file server

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"

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

    }

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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