Link to home
Start Free TrialLog in
Avatar of misnstt
misnstt

asked on

File Upload When Adding New User

I have a ASP.NET page with a Create New User Wizard. Step 3 of the wizard I have some File upload fields and a upload button which successfully uploads the files into a "Upload" folder on the Hard Drive. Also included in step 3 of the wizard is the Finish button which adds the new user information into the table.

I need to make adjustments and am unsure how to proceed. I want that when the new user presses the upload button or the Finish button a folder is created in a Upload folder within the root of the site. I want the folder name to be ased upon the new user "USER NAME". The new folder should contain the uploaded file for that user.

I am attaching the code .
Thanks
NEW USER WIZARD PAGE
<asp:WizardStep ID="CreateUserWizardStep2" runat="server" 
            Title="Upload Pictures">
            <input id="File1" runat="server" size="60" type="file"/>
            <br />
            <p id="upload-area">
   <input id="File2" type="file" runat="server" size="60" />
</p>
            <input id="AddFile" onclick="addFileUploadBox()" type="button" 
                value="Add file" /><br />
            <br />
            <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" 
                Text="Upload Now" />
                <span id="Span1" runat="server" />
                <script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;
		
    var uploadArea = document.getElementById ("upload-area");
	
    if (!uploadArea)
        return;
 
    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);
	
    var newUploadBox = document.createElement ("input");
	
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";
	
    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
	    
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic:" + addFileUploadBox.lastAssignedId);
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
        </asp:WizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
        </asp:CompleteWizardStep>
    </WizardSteps>
</asp:CreateUserWizard>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="LeftColumnContent" Runat="Server">
</asp:Content>
 
    
ASPX.VB Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim UpPath As String
        Dim UpName As String
        UpPath = "C:\UploadedUserFiles"
        UpName = Dir(UpPath, vbDirectory)
        If UpName = "" Then
            MkDir("C:\UploadedUserFiles\")
        End If
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim uploads As HttpFileCollection
        uploads = HttpContext.Current.Request.Files
 
        For i As Integer = 0 To (uploads.Count - 1)
 
            If (uploads(i).ContentLength > 0) Then
                Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)
 
                Try
                    uploads(i).SaveAs("C:\UploadedUserFiles\" + c)
                    Span1.InnerHtml = "File Uploaded Sucessfully."
                Catch Exp As Exception
                    Span1.InnerHtml = "Some Error occured."
                End Try
 
            End If
 
        Next i
 
    End Sub
End Class

Open in new window

Avatar of Marcus Keustermans
Marcus Keustermans
Flag of South Africa image

You can do the following
I assume that when the user chooses a username it is validated to make sure that you do not have duplicate usernames or the username is auto generated. That theres a user class that will hold the data

if not substitute the way you get the user info in the code snipet below to you wn way
See code in snipet
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
 
  Dim directoryName as String        
  Dim uploads As HttpFileCollection
  uploads = HttpContext.Current.Request.Files
 
  Try
    'I am not sure where you store the username
    'for this example I will retrieve it from a Session Variable
    newUser = (User)Session("NewUser")
    directoryName =MapPath(".") & "\" & newUser.userName 
    Directory.CreateDirectory(directoryName)
  Catch ex As Exception
  End Try
 
  For i As Integer = 0 To (uploads.Count - 1)
    If (uploads(i).ContentLength > 0) Then
      Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)
      Try
        uploads(i).SaveAs(directoryName & "\" & c)
        Span1.InnerHtml = "File Uploaded Sucessfully."
        Catch Exp As Exception
        Span1.InnerHtml = "Some Error occured."
      End Try
    End If
  Next i
 End Sub

Open in new window


I forgot tot tell you the following and I left something out.

newUser = (User)Session("NewUser")

The syntax for the above is C# syntax.  You need to cahnge that to VB Syntax.

as far as I know you do not need to cast in VB.

You also need to add  Dim newUser as User to the snippet.

Avatar of misnstt
misnstt

ASKER

Good morning and thanks for the valuable assistance.  I have added the code as you suggested and have errors in  "newUser = (User)Session("NewUser")"
This is because i havent changed it to VB syntax.  I am very much a beginner and dont know exactly what I need to change.
Could you please advise  and I think I will be able to finish this.

I am attaching the code I currently have.
Thanks

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim newUser As User
        Dim directoryName As String
        Dim uploads As HttpFileCollection
        uploads = HttpContext.Current.Request.Files
 
        Try
            'I am not sure where you store the username
            'for this example I will retrieve it from a Session Variable
         newUser = (User)Session("NewUser")
            directoryName = MapPath(".") & "\" & newUser.userName
            Directory.CreateDirectory(directoryName)
        Catch ex As Exception
        End Try
 
        For i As Integer = 0 To (uploads.Count - 1)
            If (uploads(i).ContentLength > 0) Then
                Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)
                Try
                    uploads(i).SaveAs(directoryName & "\" & c)
                    Span1.InnerHtml = "File Uploaded Sucessfully."
                Catch Exp As Exception
                    Span1.InnerHtml = "Some Error occured."
                End Try
            End If
        Next i
    End Sub

Open in new window

try one of these. one will work.

first, try:

newUser = directcast(Session("NewUser"), User)

or, if that doesn't work, try:

newUser = CType(Session("NewUser"), User)


DirectCast is preferred, but only useful if the object is inherently already of that type, but is being contained as a more generic type of itself. CType will convert an object (if such a conversion is available), even if the object is not inherently of that type.
Avatar of misnstt

ASKER

Good Afternoon,
I tried both of the examlpes and am still getting a error.  I am sending the detailed outline of the errors.
Thanks

c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE> "C:\Windows\Microsoft.NET\Framework\v3.5\vbc.exe" /t:library /utf8output /R:"C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Web_vgn38bej.dll" /R:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\assembly\dl3\2faf1681\00ca2aca_f8e1c801\EO.Web.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Design\2.0.0.0__b03f5f7f11d50a3a\System.Design.dll" /R:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Code.ytt77cw9.dll" /R:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\assembly\dl3\6114997e\002fd209_2a02c701\AlbumOnNet.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll" /R:"C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /out:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Web_enhancedcreateuserwizard.aspx.3f13c644.tu2x-nlq.dll" /D:DEBUG=1 /debug+ /win32resource:"C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\smll-05r.res" /define:_MYTYPE=\"Web\" /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Collections.Specialized,System.Configuration,System.Text,System.Text.RegularExpressions,System.Linq,System.Xml.Linq,System.Web,System.Web.Caching,System.Web.SessionState,System.Web.Security,System.Web.Profile,System.Web.UI,System.Web.UI.WebControls,System.Web.UI.WebControls.WebParts,System.Web.UI.HtmlControls /warnaserror- /optionInfer+  "C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Web_enhancedcreateuserwizard.aspx.3f13c644.tu2x-nlq.0.vb" "C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Web_enhancedcreateuserwizard.aspx.3f13c644.tu2x-nlq.1.vb" "C:\Users\Laptop\AppData\Local\Temp\Temporary ASP.NET Files\new folder (3)\16d64cd5\c4a25828\App_Web_enhancedcreateuserwizard.aspx.3f13c644.tu2x-nlq.2.vb"
 
 
Microsoft (R) Visual Basic Compiler version 9.0.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.
 
C:\Users\Laptop\Desktop\New Folder (3)\Membership\EnhancedCreateUserWizard.aspx.vb(106) : error BC30002: Type 'User' is not defined.
 
        Dim newUser As User
                       ~~~~
C:\Users\Laptop\Desktop\New Folder (3)\Membership\EnhancedCreateUserWizard.aspx.vb(114) : error BC30002: Type 'User' is not defined.
 
            newUser = DirectCast(Session("NewUser"), User)
                                                     ~~~~ 
C:\Users\Laptop\Desktop\New Folder (3)\Membership\EnhancedCreateUserWizard.aspx.vb(116) : error BC30451: Name 'Directory' is not declared.
 
            Directory.CreateDirectory(directoryName)
            ~~~~~~~~~         
 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marcus Keustermans
Marcus Keustermans
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial