Download Pages

Last post 05-11-2008 11:32 PM by bigjoe11a. 56 replies.

Sort Posts:

  • Download Pages

    05-09-2008, 3:17 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

    What I'm trying to do is setup a Download Page. and I have a Admin Page setup where I can Add Files and add Descriptions and So on. Nothing I have tried seems to work.

     

    PLEASE

    Does any one have an idea on how to do this.

    I want to be able to set Categories and Times Downloaded and File Sizes. Thanks

    Joe

     

  • Re: Download Pages

    05-09-2008, 4:01 PM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 106

    Your question is a little vague, all you need to allow a user to download a file is provide a link to it.

  • Re: Download Pages

    05-09-2008, 4:16 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

    No Thats not what I wanted. I wanted some thing else. I wanted some thing that I could upload and add descriptions and so on. and with a Download counter and file size and so on

     

     

  • Re: Download Pages

    05-09-2008, 4:24 PM
    • Loading...
    • kraznodar
    • Joined on 05-25-2007, 3:47 PM
    • Posts 178

    You am done confuzed us muchly!

    Uploading and downloading are two different things.  So if I understand, you want to be able to upload files to the server, add descriptions and information about the files.  Then you want to make the files available for download and keep a record of the number of downloads?

    If that is correct reply with yes and if not then specify where I'm off target.

  • Re: Download Pages

    05-09-2008, 4:34 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

     YES, Thats Right

     

  • Re: Download Pages

    05-09-2008, 4:49 PM
    • Loading...
    • kraznodar
    • Joined on 05-25-2007, 3:47 PM
    • Posts 178

    Ok then. We can do this but it may take a while to help you get it working.  First we (I and anyone else that wants to help too) need to know what your web server setup is like and how you are storing info.  Are you storing the files on the web server, a remote file server or in a database?  Are you storing the description information and the counters in a database, MS Access, or XML files?

  • Re: Download Pages

    05-09-2008, 4:56 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

     Ok, I want the file information in the sql database and the file on the hard drive. I host my own web site in PHP and in ASP.net 2.0

    I have 2 PC's and I been using my personal PC for testing asp.net and making sure that it works, then I can create what I need on my other PC that has the

    server to make the web site and then copy the files over to the directory that I want

     

  • Re: Download Pages

    05-09-2008, 5:37 PM
    • Loading...
    • kraznodar
    • Joined on 05-25-2007, 3:47 PM
    • Posts 178

    Admin page:

    Drop a fileupload control onto an aspx page. Add as many text boxes as needed for category and description.  Slap in a submit button. 

    The File upload control will look something like this:

    <asp:FileUpload ID="FileUpload" runat="server" />

     In the code behind you want something like this:

    Dim savePath As String = "C:\temp\"

    Sub Submit_Click(ByVal source As Object, ByVal e As EventArgs)

    If Not (FileUpload.PostedFile Is Nothing) Then

    Try

      Dim postedFile = FileUpload.PostedFile

      Dim filename As String = Path.GetFileName(FileUpload.FileName)

      Dim contentType As String = FileUpload.ContentType

      Dim contentLength As Integer = FileUpload.ContentLength

      Dim Description As String = Me.txtDescription.Text

      FileUpload.SaveAs(savePath & filename)

    ' I assume you know how to do an insert into SQL Server.  That code goes here

    Catch exc As Exception

      message.Text = "Failed uploading file"

    End Try

    End If

    End Sub

    For the display page you can use a gridview or an aspx table.  Run a select statement on the database and set the gridview datasource to the returned table or build the aspx table in a for loop.  Park an anchor tag around each filename with the path to the file.  There is more to it than this but I'm out of time.

  • Re: Download Pages

    05-09-2008, 6:10 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

     I get your Idea, and thats so cool. Its just that No. I have no idea on how to add the info from the file to the database. See I created a database like this

    File_ID

    Filename (This holds just the file name, This should be made into link with the file path in it)

    Filesize (This holds the file size, The size of the file)

    Description (This holds the file description of the file)

    File path (the complete path and file name)

    Category. (File Related Category, Movies, Mp3's and So on)

    I need to be able to add the description, Category and so on to the sql database. Can you tell me how to do that.

    and thank for your help

     

  • Re: Download Pages

    05-09-2008, 6:25 PM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 106

    Hello again, I can help you out with this as well.

    Public Sub InsertNewRecord(ByVal FileName As String, ByVal FileSize As Long, ByVal Description As String, ByVal FileURL As String, Category as String)

            Dim sql As New SqlDataSource()
            sql.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString()
            sql.InsertCommandType = SqlDataSourceCommandType.Text

            sql.InsertCommand = "Insert Into TableName (FileName, FileSize, Description, FileURL, Category) Values (@FileName, @FileSize, @Description, @FileURL, @Category)"
            sql.InsertParameters.Add("FileName", FileName)
            sql.InsertParameters.Add("FileSize", FileSize)
            sql.InsertParameters.Add("Description", Description)
            sql.InsertParameters.Add("FileURL", FileURL)
            sql.InsertParameters.Add("Category", Category)

            sql.Insert()

            sql.Dispose()

         End Sub

     

  • Re: Download Pages

    05-09-2008, 6:38 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

    Ok, I'm all screwed up here. below is the text code for my page. Please some one fix this

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Mangae Downloads.aspx.vb" Inherits="Admin_Mangae_Downloads" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Manage Downloads<br />
            <br />
            <br />
            &nbsp; File Description :<br />
            <asp:TextBox ID="txtDescription" runat="server" Height="106px" TextMode="MultiLine" Width="305px"></asp:TextBox><br />
            <br />
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
           
            Dim SavePath as string = "c:\temp"
           
            Sub Submit_Click(Byval source as object, ByVal e as EvenArgs)
            if Not (FileUpload.PostedFile) Then
           
            Try
            Dim filename as string = Path.GetFileName(FileUpload.FileName)
            Dim ContentType as string = FileUpload.ContentLength
            Dim ContentLenght as Inter = FileUpload.ContentLength
            Din Description as string = txtdecription.text
            FileUpload.Saveas(SavePath & FileName)
           
            'This is where I would save to sql database
           
            Catch exc as Exception
            message.Text = "Failed Uploading File"
           
            end Try
            End if
           
            end Sub
         
           
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" /></div>
        </form>
    </body>
    </html>
     

    I have no idea on what I'm doing wrong

     

  • Re: Download Pages

    05-09-2008, 7:11 PM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 106

    OK, first since you are using a code behind that is where you should have put that code.

    Your .aspx file is should look something like this.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Mangae Downloads.aspx.vb" Inherits="Admin_Mangae_Downloads" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Description<br />
            <asp:TextBox ID="txtDescription" runat="server"></asp:TextBox><br />
            Category<br />
            <asp:TextBox ID="txtCategory" runat="server"></asp:TextBox><br />
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" /></div>
        </form>
    </body>
    </html>
     

    Now your code behind should look like this

    Imports System.IO

    Partial Class Admin_Mangae_Downloads
        Inherits System.Web.UI.Page

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            DoUpload()
        End Sub

        Protected Sub DoUpload()

            Dim SavePath As String = "~/"
            Dim UploadFile As String = _
                 Server.MapPath(SavePath & FileUpload1.FileName)

            Dim NoPath As String = Path.GetFileName(FileUpload1.FileName)

            Try
                InsertNewRecord(NoPath, GetFileSize(FileUpload1.FileName), txtDescription.Text, UploadFile, txtCategory.Text)
                If FileUpload1.HasFile Then FileUpload1.PostedFile.SaveAs(UploadFile)
            Catch ex As Exception
                MsgBox("There was a problem: " & Err.Description)
            End Try

        End Sub

        Public Sub InsertNewRecord(ByVal FileName As String, ByVal FileSize As Long, ByVal Description As String, ByVal FileURL As String, ByVal Category As String)

            Dim sql As New SqlDataSource()
            sql.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString()
            sql.InsertCommandType = SqlDataSourceCommandType.Text

            sql.InsertCommand = "Insert Into TableName (FileName, FileSize, Description, FileURL, Category) Values (@FileName, @FileSize, @Description, @FileURL, @Category)"
            sql.InsertParameters.Add("FileName", FileName)
            sql.InsertParameters.Add("FileSize", FileSize)
            sql.InsertParameters.Add("Description", Description)
            sql.InsertParameters.Add("FileURL", FileURL)
            sql.InsertParameters.Add("Category", Category)

            sql.Insert()

            sql.Dispose()

        End Sub

        Protected Function GetFileSize(ByVal MyFile As String)

            Dim nFileInfo As New FileInfo(Server.MapPath(MyFile))

            Dim FileInBytes As Long = nFileInfo.Length
            Dim FileInKB As Long = nFileInfo.Length / 1024

            GetFileSize = FileInBytes

        End Function
    End Class

    Try to get that working for now, should be able to simply copy paste code for each file.

  • Re: Download Pages

    05-09-2008, 8:11 PM
    • Loading...
    • bigjoe11a
    • Joined on 05-09-2008, 3:11 PM
    • Posts 55

    Thank You. The only problem I have is that its not uploading the file. I see in your  sample you have

    Dim SavePath As String = "~/"
    Dim UploadFile As String = _

    Is there a way to change this too Dim SavePath As String = "c:\temp\"

    so that it will work. It trying to upload to my admin folder. Just errors out telling me that the file is not fount


          

  • Re: Download Pages

    05-09-2008, 8:36 PM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 106

    That is not what the problem was, try this code. I didn't actually create a database so couldn't test some of the code.

    Imports System.IO

    Partial Class Admin_Mangae_Downloads
        Inherits System.Web.UI.Page

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            DoUpload()
        End Sub

        Protected Sub DoUpload()

            Dim SavePath As String = "~/"
            Dim UploadFile As String = _
                 Server.MapPath(SavePath & FileUpload1.FileName)

            Dim NoPath As String = Path.GetFileName(FileUpload1.FileName)

            Try
                Dim X As Long = GetFileSize(FileUpload1.PostedFile.FileName)

                'If you get another error comment the next line out for sample purposes this will get you started at least then we'll deal with your database
                InsertNewRecord(NoPath, X, txtDescription.Text, UploadFile, txtCategory.Text)


                If FileUpload1.HasFile Then FileUpload1.PostedFile.SaveAs(UploadFile)
            Catch ex As Exception
                MsgBox("There was a problem: " & Err.Description)
            End Try

        End Sub

        Public Sub InsertNewRecord(ByVal FileName As String, ByVal FileSize As Long, ByVal Description As String, ByVal FileURL As String, ByVal Category As String)

            Dim sql As New SqlDataSource()
            sql.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString()
            sql.InsertCommandType = SqlDataSourceCommandType.Text

            sql.InsertCommand = "Insert Into TableName (FileName, FileSize, Description, FileURL, Category) Values (@FileName, @FileSize, @Description, @FileURL, @Category)"
            sql.InsertParameters.Add("FileName", FileName)
            sql.InsertParameters.Add("FileSize", FileSize)
            sql.InsertParameters.Add("Description", Description)
            sql.InsertParameters.Add("FileURL", FileURL)
            sql.InsertParameters.Add("Category", Category)

            sql.Insert()

            sql.Dispose()

        End Sub

        Protected Function GetFileSize(ByVal MyFile As String) As Long

            Dim nFileInfo As New FileInfo(MyFile)

            Dim FileInBytes As Long = nFileInfo.Length
            Dim FileInKB As Long = nFileInfo.Length / 1024

            GetFileSize = FileInBytes

        End Function
    End Class

  • Re: Download Pages

    05-09-2008, 9:10 PM