ASP.NET - to copy folders from local machine to folder in server

Asked By anbu n on 21-Sep-11 06:23 AM

to copy the complete files and folders ,  from local machine , i.e, folder/directory path which is selected by user has to be completely[all files within the path is be selected] is to be pasted/copied into  folder which is in webserver where the web application has been hosted.

Reena Jain replied to anbu n on 21-Sep-11 06:41 AM
Hi.

You can not upload folder on server either you can upload zip or files
  1. you can upload the file on sever, can not do copy
  2. there should not be any special character in path or you can say path should not contain any special character with it.
Rather you can upload multiples file. For this just import the System.IO class into your ASP.NET page, and to then use the HttpFileCollection class to capture all the files that are sent in with the Request object. This approach enables you to upload as many files as you want from a single page.

Code Example

<%@ Import Namespace="System.IO" %>
<%@ Page Language="VB" %>
 
<script runat="server">
 
   
Sub SubmitButton_Click(Source As Object, e As EventArgs)
   Dim filepath As String = "C:\Uploads"
   Dim uploadedFiles As HttpFileCollection = Request.Files
   Dim i As Integer = 0
   
   Do Until i = uploadedFiles.Count
    Dim userPostedFile As HttpPostedFile = uploadedFiles(i)
      
    Try
     If (userPostedFile.ContentLength > 0) Then
    Span1.InnerHtml += "<u>File #" & (i+1) & "</u><br>"
    Span1.InnerHtml += "File Content Type: " & _
       userPostedFile.ContentType & "<br>"
    Span1.InnerHtml += "File Size: " & _
       userPostedFile.ContentLength & "kb<br>"
    Span1.InnerHtml += "File Name: " & _
       userPostedFile.FileName & "<br>"
    
    userPostedFile.SaveAs(filepath & "\" & _
       Path.GetFileName(userPostedFile.FileName))
    
    Span1.InnerHtml += "Location where saved: " & _
       filepath & "\" & _
       Path.GetFileName(userPostedFile.FileName) & _
       "<p>"
     End If
    Catch ex As Exception
     Span1.InnerHtml += "Error:<br>" & ex.Message
    End Try
    i += 1
   Loop   
  End Sub

 
</script>
 
<html>
<head>
</head>
<body>
  <form enctype="multipart/form-data" runat="server">
  <p>
    Select File1:<br />
    <input id="File1" type="file" runat="Server" />
    <br />
    Select File2:<br />
    <input id="File2" type="file" runat="Server" />
    <br />
    Select File3:<br />
    <input id="File3" type="file" runat="Server" />
    <br />
    Select File4:<br />
    <input id="File4" type="file" runat="Server" />
  </p>
  <p>
    <input id="Submit1" type="submit" value="Upload Files"
     runat="Server" onserverclick="SubmitButton_Click" />
    <br />
  </p>
  <span id="Span1" runat="Server"></span>
  </form>
</body>
</html>


hope this will help you
Riley K replied to anbu n on 21-Sep-11 01:26 PM
You didn't say if the server is in the same domain as the client. In other
words, is this inside a corporate network? If so, simply use UNC path names
to copy the file using built-in methods in the framework. See
System.IO.File.Copy
http://msdn2.microsoft.com/en-us/library/system.io.file.copy(VS.80).aspx

If you are on different networks, then you need the server to have a
protocol running to accept the file. Two most common are FTP and HTTP (Web
Server).

For FTP, see this:
http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest(VS.80).aspx

If you want to upload to a web server, then you will need to post the file
to the web server. Since you have client code, you won't be doing this from
a web page, but that doesn't mean you cannot use the web protocols. If you
want to look into this route, start with:
http://msdn2.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx


Cheers
anbu n replied to Riley K on 26-Sep-11 03:25 AM
hi Riley K ,

folders and files within, are to be copied from local machine to folders inside the root directory in web server where the application has been hosted

kindly advise, how to fetch folders as well as its inner folders & files from local machine to  the web server where the application has been hosted