Solved

asp.net upload

Posted on 2012-03-29
3
592 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
Comment Utility
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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now