Solved

asp.net upload

Posted on 2012-03-29
3
612 Views
Last Modified: 2012-03-30
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
Comment
Question by:Webboy2008
3 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 37784321
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
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37784331
Pretty-much what Luck85 said. Do this:
Change:
 if ((FileExt == ".pdf"))

to

if ( !string.IsNullOrEmpty( FileExt ) )
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 37785438
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question