Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

asp.net upload

For some reasons, when upload .pdf is working fine. but when .txt, doc, docx does not do anything....We use ajax 1.0 and framework 2.

Any reasons why and how to fix it?

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadNow.ascx.cs" Inherits="UI_UploadNow" %>
<%@ Register Assembly="obout_Interface" Namespace="Obout.Interface" TagPrefix="cc1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<%@ Register TagPrefix="obout" Namespace="Obout.ComboBox" Assembly="obout_ComboBox" %>
<style type="text/css">
    .MasterWidth
    {
        width: 930px;
               
    }
</style>
<div id="centeredmenu">        
        <ul>
        <li><a class="active" href="UploadNow.aspx">Upload Outlines Now</a></li>
        </ul>
</div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
        <br /><br />
        <table cellpadding="5" cellspacing="5">
        <tr>
        <td colspan="2">
        <fieldset class="MasterWidth">
        <br />
        <asp:Label ID="StatusLabel" runat="server" Text=""></asp:Label>
        <table>        
        <tr><td>
        <strong>Law School</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator_LawSchool" runat="server"
        ErrorMessage="*" ControlToValidate="TxtSchool01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox ID="TxtSchool01"
        Width="400" FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"
        runat="server" Text=""></cc1:OboutTextBox>
       
        <cc2:AutoCompleteExtender
        runat="server"
        ID="AutoCompleteExtender_SchoolList" ServicePath="~/AutoComplete.asmx"
        ServiceMethod="GetSchoolCompletionList" TargetControlID="TxtSchool01"
        CompletionListCssClass="autocomplete_completionListElement"
        CompletionListItemCssClass="autocomplete_listItem"
        CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
        MinimumPrefixLength="2">
        </cc2:AutoCompleteExtender>
        </td>
        <td><strong>Class</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator_TxtClass01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtClass01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtClass01" Width="285" runat="server"></cc1:OboutTextBox>
        </td>
        </tr>
        <tr>        
        <td>
        <strong>Professor</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator_TxtProfessor01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtProfessor01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtProfessor01" Width="285" runat="server"></cc1:OboutTextBox>
        <cc1:OboutTextBox  Visible="false" ID="TxtMajor01" Width="285" runat="server" Text="Law"></cc1:OboutTextBox>
        </td>
        <td>    
        <strong>Textbook</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1_TxtTextbook01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtTextbook01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtTextbook01" Width="285" runat="server"></cc1:OboutTextBox>
        </td>
        </tr>
        <tr>
        <td><strong>Semester/Quarter</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator_TxtSememter01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtTextbook01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtSememter01" Width="285" runat="server"></cc1:OboutTextBox>
        </td>
        <td><strong>Year</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1_TxtYear01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtYear01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtYear01" Width="285" runat="server"></cc1:OboutTextBox>
        </td>
        </tr>
        <tr>
        <td>
        <strong>Total Number of Page</strong>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1_TxtTotalPageNumber01" runat="server"
        ErrorMessage="*" ControlToValidate="TxtTotalPageNumber01" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <cc1:OboutTextBox FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutTextBox"  ID="TxtTotalPageNumber01" Width="285" runat="server"></cc1:OboutTextBox>        
        </td>
        <td>
        <strong>Upload files</strong><br />
        <asp:FileUpload  CssClass="" id="FileUploadControl" runat="server" />&nbsp;
        </td>
        </tr>
        <tr>
        <td><br /></td>
        <td>
        <cc1:OboutButton
        FolderStyle="../Services/UIStyleControls/Obout/Interface/styles/plain/OboutButton"
        ID="UploadButton"  
        Text="Upload Now"
        ToolTip="Upload Now"
        onclick="UploadButton_Click"
        runat="server"></cc1:OboutButton>
        </td>
        </tr>
        </table>  
        </fieldset>
        </td>
        </tr>
        </table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UploadButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SdsCollegeList" runat="server"
    ConnectionString="<%$ ConnectionStrings:OutlinesDatabaseConnection %>"
    SelectCommand="SELECT [SchoolName], [SchoolId], [CollegeId] FROM [Schools] WHERE ([StatusId] = @StatusId) And (SchoolName <> '')">
    <SelectParameters>
        <asp:Parameter DefaultValue="1" Name="StatusId" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

//Code Behind

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile == true)
        {
            string FileExt = System.IO.Path.GetExtension(FileUploadControl.FileName);
            string UploadToLocation = ConfigurationManager.AppSettings["UploadToLocation"];
            if ((FileExt == ".pdf"))
            {
                //Step 1: Upload To the Server
                StatusLabel.Visible = true;
                //"C:\\HostingSpaces\\admin1\\kemkoaerospace.com\\wwwroot\\Aviation\\AdminZone\\"
                //FileUploadControl.SaveAs(UploadToLocation + FileUploadControl.FileName);
                FileUploadControl.PostedFile.SaveAs(@"C:\test\" + System.IO.Path.GetFileName(FileUploadControl.PostedFile.FileName));
                //StatusLabel.Text = "File Name: " + FileUploadControl.PostedFile.FileName + "<br>" +
                //FileUploadControl.PostedFile.ContentLength + "<br>" +
                //FileUploadControl.PostedFile.ToString() + "<br>" +
                //FileUploadControl.PostedFile.ContentType + "<br>";
                StatusLabel.Text = "Thank you. You successfully uploaded an outline.";
                //===============================================
                //Step 2: Save the transaction into the database
                //===============================================
                //Step 3: Email to the site owner - from user to owner
                  }
            else
            {
                StatusLabel.Text = "Invalid Extension!";
                StatusLabel.Visible = true;
            }
        }
    }
0
Webboy2008
Asked:
Webboy2008
1 Solution
 
Anil GolamariCommented:
Looking at code behind you are trying to upload only files with .pdf ext and for rest of the files you are trying to throw error message. There is an if statement in there which is trying to equate it to .pdf.  

Let me know if I misunderstood your question.
0
 
Daniel Van Der WerkenCommented:
Pretty-much what Luck85 said. Do this:
Change:
 if ((FileExt == ".pdf"))

to

if ( !string.IsNullOrEmpty( FileExt ) )
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
In agreement to both above comments by expert, I would like to add code line for .txt, doc, docx files:

if ((FileExt == ".pdf" || FileExt==".txt" || FileExt==".doc" || FileExt==".docx"))
            {
                //Step 1: Upload To the Server
                StatusLabel.Visible = true;

Open in new window


Now when you try to upload other extension files (as allowed in code), they all would get uploaded.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now