hello,
iam a newbie in the .net world recently i have been assigned a project by my organization to make a portal using vb.net which would list all the files and folders kept at the server, and i need to add a functionality wherein on a single click the user should be able to download all the files present in a particular a listbox which contains various files selected for the download, iam using file.copy method for this as the files are to be copied in the intranet only but this works fine when i run it on my computer but as soon as i deploy it and in the client server architecture the files are not copied even to the server nor in the client, although no error is shown. I have tried catching the hostname and then copying file to the host's drive but that also doesnt works too after deployment plzz help..

the code iam using is as follows:

Protected Sub btndownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndownload.Click
    Dim i As Integer
    'Dim s(i) As ArrayList
    'Dim req As String
    'req = Request.ServerVariables("REMOTE_ADDR")
    Dim TxtLocalSysName As String = Request.UserHostName
    For i = 0 To lstdownload.Items.Count - 1

        'If i <> 0 Then
        Dim filePath As String = Me.Label5.Text + "\" + lstdownload.Items(i).Text
        Dim targetFile As System.IO.FileInfo = New System.IO.FileInfo(filePath)
        Dim objFileInfo As FileInfo
        objFileInfo = New FileInfo(filePath)

        'Dim di As DirectoryInfo = New DirectoryInfo("\\" & TxtLocalSysName & "\c:\ftp420\")
        Try
            '    ' Determine whether the directory exists. 
            '    If Not di.Exists Then
            '        ' Indicate that it already exists. 
            '        di.Create()
            '        If FileAttributes.ReadOnly.CompareTo(di.Attributes) Then
            '            di.Attributes &= Not FileAttributes.ReadOnly
            '        End If
            '        ' Response.Write("The directory was created successfully.")
            '        File.Copy(filePath, "\\" & TxtLocalSysName & "\c:\ftp420\" & objFileInfo.Name)
            '        filePath = " "

            '        ' Continue For
            '        'Response.Write("That path exists already.")
            '        'Return
            '    Else
            File.Copy(filePath, "\\" & TxtLocalSysName & "\c$\downloads\" & objFileInfo.Name)
            'End If

            ' Try to create the directory. 

            ' Delete the directory. 
            ' di.Delete()
            '  Response.Write("The directory was deleted successfully.")

        Catch ex As Exception
            Console.WriteLine("The process failed: {0}", ex.ToString())
        End Try
        ' i = i - 1
        'End If


    Next i

End Sub

Edited 3 Years Ago by Nick Evan: Fixed formatting

Are there any errors being thrown?

A while ago I was facing a similar situation, but in reverse.
I added a feature for a web-portal where users could upload files to the server, and those files needed to be moved to a fileserver located behind two firewalls and the DMZ.

This is my solution.
After the file has been uploaded to the server, I immediately convert the file into a byte array and send it to a webservice, which in turn have limited access to a single predefined folder on the fileserver.

So here's what I suggest.
Create a webservice with a method that stream reads the desired file/s into a byte array, send the array, and then reconstruct the byte array into a file on the target system.
It may seem like a round-about way of doing it, but it's actually quite fast.

The reason for why it's not working for you might be as simple as a credential mismatch. Ie, the IIS_USR or ASPNET user on the server may not have read/copy rights.

thanks oxiegen for your reply... nopes it's not throwing any errors... i also thought of using the byte array.. but iam not able to put that concept into the code please help me out..

Ok. Then do some experimenting. :)

This may look weird, but it might just work.
Let's check to see if the buffer gets filled.

Protected Sub btndownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndownload.Click
   Dim i As Integer
   Dim TxtLocalSysName As String = Request.UserHostName
   Dim readStream As FileStream
   Dim writeStream As FileStream
   Try
      For i = 0 To lstdownload.Items.Count - 1
         Dim filePath As String = Me.Label5.Text + "\" + lstdownload.Items(i).Text
         Dim targetFile As System.IO.FileInfo = New System.IO.FileInfo(filePath)
         readStream = New FileStream(filePath, FileMode.Open)
         Dim length As Integer = Convert.ToInt32(readStream.Length)

         'This is the buffer.
         Dim byteFile() As Byte = New Byte(length) {}

         readStream.Read(byteFile, 0, length)
         readStream.Close()

         Dim localPath As String = "\\" & TxtLocalSysName & "\c$\downloads"
         If Not Directory.Exists(localPath) Then
            Directory.CreateDirectory(localPath)
         End If

         writeStream = New FileStream(localPath & "\" & targetFile.Name, FileMode.Create)
         writeStream.Write(byteFile, 0, length)
         writeStream.Close()
      Next i
   Catch ex As Exception
      Console.WriteLine("The process failed: {0}", ex.ToString())
   Finally
      readStream.Close()
      readStream.Dispose()
      writeStream.Close()
      writeStream.Dispose()
   End Try
End Sub

hey oxiegen, thanks for the code, its working superb in my local computer i just need to deploy it and check it in the my organization's LAN hope it works there too... :)

hey got a problem after deploying it, and opening it in the browser when i click on the download button i get this error:


Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 194: Console.WriteLine("The process failed: {0}", ex.ToString())
Line 195: Finally
Line 196: readStream.Close()
Line 197: readStream.Dispose()
Line 198: writeStream.Close()


Source File: C:\inetpub\wwwroot\freshfddser\Main.aspx.vb Line: 196

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
_Default.btndownload_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\freshfddser\Main.aspx.vb:196
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3614


please help... what to do..

hey further more when i removed the finnaly block and then deployed it...

then it was same as my original problem i.e. the program executed without showing any error but no files were downloaded... what should i do...

First you need to check if the buffer contains any data.
Put a label on the form and add this code between the read and the write.

Label1.Text = byteFile.Length()

The second thing to check see is if sharing is activated on the client computer, and thus if the share c$ exists.
Type the command NET SHARE in the command prompt.

i did tried the NET SHARE command it showed my c drive as default share, still when iam deploying it... it shows no error but doesnt even works in the server machine :( i logged as administrator had all the rights still i cant download even on my own machine... :(

I downloaded my vs 2005 from the internet is that causing a problem?? cuz its not licensed....

Nah. That's not the problem.
Although you should seriously consider buying it, regardless.

Did you add that label to the form, and the codesnippet?
Did you get any numbers in there?

the main thing is as long as it is in the windows it working but after deployment i dont know what is the problem neither the folder is being created not the files are downloaded, i thot i might not be having the permissn to write on the drives of the client but the scenario was the same even when i got the admin rights also the c$ is on default share

It doesn't matter if you log on the client computer as administrator or not.
Because it is the WEBSERVERS user account that needs access to the local computer.
Thus, if the user accounts IIS_IUSRS and/or ASPNET don't have write access to the local computer, then nothing will be copied.

I did some googling on this matter, and it seems like you need to do some javascripting to make this work.
Or, as another article states, ZIP the files and download that single file.

This article has been dead for over six months. Start a new discussion instead.