views:

847

answers:

6

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

http://stackoverflow.com/questions/263518/c-uploading-files-to-file-server


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"

+2  A: 

Why are you using a WebClient for a File.Copy?

leppie
I used this at first, didnt workout for me so tried the File.Copy..byte[] arrReturn = client.UploadFile(addy, filePath);
Imcl
A: 

for File.copy try this code...or please specify what error are you getting..

Imports System 
Imports System.Runtime.InteropServices 
Imports System.Security.Principal 
Imports System.Security.Permissions 
Public Class Form1 
    <DllImport("advapi32.DLL", SetLastError:=True)> _ 
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _ 
        ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _ 
        ByRef phToken As IntPtr) As Integer 
    End Function 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim admin_token As IntPtr 
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent() 
        Dim wid_admin As WindowsIdentity = Nothing 
        Dim wic As WindowsImpersonationContext = Nothing 
        Try 
            MessageBox.Show("Copying file...") 
            If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then 
                wid_admin = New WindowsIdentity(admin_token) 
                wic = wid_admin.Impersonate() 
                System.IO.File.Copy("C:\right.bmp", "\\157.60.113.28\testnew\right.bmp", True) 
                MessageBox.Show("Copy succeeded") 
            Else 
                MessageBox.Show("Copy Failed") 
            End If 
        Catch se As System.Exception 
            Dim ret As Integer = Marshal.GetLastWin32Error() 
            MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString()) 
            MessageBox.Show(se.Message) 
        Finally 
            If wic IsNot Nothing Then 
                wic.Undo() 
            End If 
        End Try 
    End Sub 
End Class
BreakHead
A: 

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
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
I actually want to copy/transfer files from the local machine to the remote machine.
Imcl
I think you provided the code for a Window-Based Application. Its a Web-Based Application that I am working on.
Imcl
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
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
THIS IS THE ERROR I GET :- "An exception occurred during a WebClient request"
Imcl
A: 

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

    }
kalyan
A: 

I solved it...Contact me if anyone wants a solution to it...

Imcl
A: 

How did you do it? Im trying to upload to a remote web hosting.

Manuel