Solved

asp.net upload

Posted on 2012-03-29
3
604 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

19 Experts available now in Live!

Get 1:1 Help Now