views:

685

answers:

3

I basically want to transfer a file from the client to the file storage server without actual login to the server so that the client cannot access the storage location on the server directly. I can do this only if i manually login to the storage server through windows login. I dont want to do that. This is a Web-Based Application.

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

} 

The following line doesn't execute...

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

This is the error I get:

An exception occurred during a WebClient request

+1  A: 

Ah, it seems (and with good reason), the FileUpload can only save files to the web server and its drives. So my first thought won't work.

But: if you have the necessary permissions, couldn't you just save the file that you get in the FileUpload to that UNC path using standard System.IO calls?? Something like :

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
        string completeFileName = 
           Path.Combine(@"\\192.168.1.3\upload", FileUpload1.FileName); 

        BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);

        FileStream fstm = new FileStream(completeFileName, FileMode.Create, FileAccess.ReadWrite);
        BinaryWriter bw = new BinaryWriter(fstm);

        byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
        br.Close();

        bw.Write(buffer);
        bw.Flush();
        bw.Close();
    } 
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.Message); 
    } 
} 

If you expect very large files to be uploaded, you might want to transfer the data from the BinaryReader to the BinaryWriter in chunks - instead of allocating just a single buffer - but that's just an implementation detail, really.

marc_s
It gives me this error:-Logon failure: unknown user name or bad password.
Imcl
@Imcl: yes, seems the FileUpload control can't write to a remote location - updated my answer
marc_s
this line isnt executed and it enters the catch block showing the same error as above:-FileStream fstm = new FileStream(completeFileName, FileMode.Create, FileAccess.ReadWrite);
Imcl
I even added these lines in the try block:-WebClient client = new WebClient();NetworkCredential nc = new NetworkCredential(uName, password);client.Credentials = nc;uName and pasword are strings containing the username and password of the remote/server machine
Imcl
@Imcl: well, then you obviously do not have permissions to access that path and create a file there, I would think! Can you give us the full exception (ex.ToString()) ?? Update your original post with it, please - do **not** post it here in the comments - too hard to read.
marc_s
A: 

This is what i tried to do:-

try
{
   WebClient client = new WebClient();

   NetworkCredential nc = new NetworkCredential(uName, password);
   client.Credentials = nc;

   string completeFileName =
           Path.Combine(@"\\192.168.1.3\upload", FileUpload1.FileName);

   BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);

   FileStream fstm = new FileStream(completeFileName, FileMode.Create, FileAccess.ReadWrite);

   BinaryWriter bw = new BinaryWriter(fstm);

   byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
   br.Close();

   bw.Write(buffer);
   bw.Flush();
   bw.Close();
}
catch (Exception ex)
{
        Console.WriteLine(ex.Message);
} 

and It gives me this error:-

Logon failure: unknown user name or bad password.

i have the required permissions.

Imcl
A: 

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

Imcl