简体   繁体   中英

Upload files in vb.net

I have a code in vb.net (as shown below) in which it ask to upload a file after the selection of yes button. On checking No button, it doesn't ask us to upload a file as shown below in the images.

************************************************VB Code******************************************************

    Protected Sub rblOrgChart_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rblOrgChart.SelectedIndexChanged
            ruOrgChart.Visible = False
            hlOrgChart.Visible = False


            ' btnOrgChart.Visible = rblOrgChart.SelectedValue And bolHasWritePermission
            hlOrgChart.Visible = rblOrgChart.SelectedValue

            If CBool(rblOrgChart.SelectedValue) Then
                ruOrgChart.Visible = True

                Dim dtFile As DataTable
                dtFile = dalClientProfile.GetFileUpload(CInt(Session("OrgID")), CInt(Session("LicenseeID")), enumFileTypeUpload.ORG_CHART)
                If dtFile.Rows.Count > 0 Then
                    Dim dr As DataRow = dtFile.Rows(0)
                    hlOrgChart.NavigateUrl = ruOrgChart.TargetFolder & "/" & dr.Item("FileName")
                    hlOrgChart.Text = dr.Item("FileName")

                    hlOrgChart.Visible = True
                End If
            Else
                ' If Me.lblOrgChartMissing2.Visible Then Me.lblOrgChartMissing2.Visible = False
            End If

        End Sub

The code inside the method btnOrgChart_Click is:

Protected Sub btnOrgChart_Click(sender As Object, e As System.EventArgs) Handles btnOrgChart.Click
    Try

        'If ruOrgChart.InvalidFiles.Count > 0 Then
        '    lblOrgChartNotPDF.Visible = True
        'Else
        '    lblOrgChartNotPDF.Visible = False
        'End If

        '------------------------------------------------------------------
        ' Q07. HAS WRITTEN ORG. CHART [IAPData..tblReadinessProfile_Question ID=9 ]
        '------------------------------------------------------------------



        If (rblOrgChart.SelectedValue = 1 And ruOrgChart.UploadedFiles.Count > 0) Then
            hlOrgChart.Visible = True
            '' ''======' BEGIN NEW CODE --- to upload file to virtual directory
            Dim sufile As Telerik.Web.UI.UploadedFile = Nothing
            For Each sufile In Me.ruOrgChart.UploadedFiles

                ' for now upload files on webserver
                'If AppSettings.Item("Environment") <> "Prod" Then
                remoteName = Request.PhysicalApplicationPath
                'Else
                'remoteName = AppSettings.Item("UploadRootPath").ToString.Trim()
                'End If

                Dim strPhysicalFilePath As String = remoteName & AppSettings.Item("FileUploadRootPath").ToString & "\" & dtLicensee.Rows(0).Item("UniqueIdentifier").ToString & "\" & dtOrg.Rows(0).Item("OrgCode").ToString & "\"

                CreateFileUploaderLink(remoteName)

                If Not Directory.Exists(strPhysicalFilePath) Then
                    Directory.CreateDirectory(strPhysicalFilePath)
                End If

                hlOrgChart.NavigateUrl = AppSettings.Item("FileUploadRootPath").ToString & "/" & dtLicensee.Rows(0).Item("UniqueIdentifier").ToString & "/" & dtOrg.Rows(0).Item("OrgCode").ToString & "/" & ruOrgChart.UploadedFiles(0).GetName()
                dalClientProfile.SaveFileUpload(CInt(Session("OrgID")), CInt(Session("LicenseeID")), enumFileTypeUpload.ORG_CHART, ruOrgChart.UploadedFiles(0).GetName(), strPhysicalFilePath, True)

                hlOrgChart.Text = ruOrgChart.UploadedFiles(0).GetName()
                Dim NewFileName As String
                NewFileName = sufile.GetName()
                'If File.Exists(strPhysicalFilePath & NewFileName) Then
                '    Dim script As String = "alert('" + Me.GetLocalResourceObject("err.fileuploaded.text") + "');"
                '    ScriptManager.RegisterStartupScript(ruOrgChart, ruOrgChart.GetType(), "clientscript", script, True)
                '    Exit Sub
                'End If
                sufile.SaveAs(strPhysicalFilePath & NewFileName, True)

                ' Cancel the connection 
                RemoveFileUploaderLink(remoteName)
            Next
        End If
    Catch ex As Exception
        Dim oErr As New ErrorLog(ex, "Error in btnOrgChart_Click")
        Response.Redirect("~/Error/ErrorPage.aspx?type=Err&ui=" & Request.QueryString("ui"), True)
        Exit Sub
    End Try

End Sub   


**********************************************Images*******************************************************

在此处输入图片说明

在此处输入图片说明

As shown above in the images, on selection of yes it ask us to upload a file. By default, it always remains No as the value of HasOrgChart is always 0.

 rblOrgChart.SelectedValue = IIf(CBool(dtOrg.Rows(0).Item("HasOrgChart")), 1, 0)


******************************************************HTML*************************************************

The .aspx code belonging to the above images and to the above vb codes are:

<div class="grid-c2">
   <div>
      <asp:UpdatePanel runat="server" ID="Updatepanel1" UpdateMode="Conditional">
         <Triggers>
            <asp:AsyncPostBackTrigger controlid="rblOrgChart" EventName="SelectedIndexChanged"/>
         </Triggers>
         <ContentTemplate>
            <asp:RadioButtonList ID="rblOrgChart" runat="server" AutoPostBack="True">
               <asp:ListItem ID="rblOrgChartY"  runat="server" Value="1" Text="Yes (Please upload the organizational chart as a PDF file)"></asp:ListItem>
               <asp:ListItem  ID="rblOrgChartN"  runat="server" Value="0" Text="No" ></asp:ListItem>
            </asp:RadioButtonList>
            <div>
               <br />          
               <telerik:RadAsyncUpload ID="ruOrgChart" runat="server" AutoPostBack="true"
                  HideFileInput="True"                                                            
                  AllowedFileExtensions=".pdf"
                  OnClientValidationFailed="validationFailed" 
                  onclientfileuploaded="ruOrgChart_Fileuploaded" 
                  MaxFileInputsCount="1"
                  InitialFileInputsCount="1"
                  MaxFileSize="5000000"
                  />
               <div class="fileLink">
                  <asp:HyperLink runat="server" ID="hlOrgChart" Target="_blank" Visible="false" 
                     Text=""></asp:HyperLink>
               </div>
            </div>
            <br />
            <div style="display:none">
               <asp:linkButton ID="btnOrgChart" CssClass="btnUpload" runat="server" Text=""   />
            </div>
            <br />
         </ContentTemplate>
      </asp:UpdatePanel>
   </div>
</div>

I am wondering what changes do I need to make in the vb.net and .aspx codes so that instead of Yes and No radio button, there is only select button which ask us directly to upload a file with no yes/no options.

You already have a select button on your form. Why not bypass the Yes/No and just use that?

You should have a Sub btnOrgChart_Click code somewhere. Your variable names don't match with your intended actions, but I think that under the Click code, you might want this somewhere:

       ruOrgChart.Visible = True

        Dim dtFile As DataTable
        dtFile = dalClientProfile.GetFileUpload(CInt(Session("OrgID")), CInt(Session("LicenseeID")), enumFileTypeUpload.ORG_CHART)
        If dtFile.Rows.Count > 0 Then
            Dim dr As DataRow = dtFile.Rows(0)
            hlOrgChart.NavigateUrl = ruOrgChart.TargetFolder & "/" & dr.Item("FileName")
            hlOrgChart.Text = dr.Item("FileName")

            hlOrgChart.Visible = True
        End If

EDIT: I believe, to remove the radio button section, remove:

    <asp:RadioButtonList ID="rblOrgChart" runat="server" AutoPostBack="True">
       <asp:ListItem ID="rblOrgChartY"  runat="server" Value="1" Text="Yes (Please upload the organizational chart as a PDF file)"></asp:ListItem>
       <asp:ListItem  ID="rblOrgChartN"  runat="server" Value="0" Text="No" ></asp:ListItem>
    </asp:RadioButtonList>

and

     <Triggers>
        <asp:AsyncPostBackTrigger controlid="rblOrgChart" EventName="SelectedIndexChanged"/>
     </Triggers>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM