Sample Page I like to share

Feb 7, 2009 at 10:23 AM
Edited Feb 7, 2009 at 10:25 AM
I have seen a few things here that I was have problems with and used some code that other user here have tried to use or sent here.
I have put together a sample page that may be of some use to others. This is coded in VB since I use vb.


[CODE START]

<%@ Page Language="VB" Title="File Uploader Sample" %>
<%@ Import Namespace="System.IO" %>
<%@ Register Assembly="com.flajaxian.FileUploader" Namespace="com.flajaxian" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/jscript">
function FileStateChanged(uploader, file, httpStatus, isLast){
    Flajaxian.fileStateChanged(uploader, file, httpStatus, isLast);
    if(isLast){
           document.location.href = document.location.href;
    }
}
</script>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateFolders()
    End Sub
    Protected Sub CreateFolders()
        '************************************************
        'this will create folder that are need for this page
        'upload and a sub folder thumbs
        'The folders are related to where the page file is
        '************************************************

        'Create the Upload directory if it does not exist.
        Dim strUploadFolder As String
        Dim myUploadpath As String = Server.MapPath(".")
        strUploadFolder = myUploadpath + "/Upload/"
        If (Not Directory.Exists(strUploadFolder)) Then
            Directory.CreateDirectory(strUploadFolder)
        End If

        'Create the thumbs directory if it does not exist.
        Dim strthumbsFolder As String
        Dim mythumbspath As String = Server.MapPath(".")
        strthumbsFolder = mythumbspath + "/Upload/thumbs/"
        If (Not Directory.Exists(strthumbsFolder)) Then
            Directory.CreateDirectory(strthumbsFolder)
        End If
    End Sub
    Protected Sub Repeater1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        
        Dim DI As New System.IO.DirectoryInfo(Server.MapPath("./Upload/thumbs"))
        
        Dim DT As New System.Data.DataTable()
        DT.Columns.Add("FileName")
        Dim DR As System.Data.DataRow
        For Each FI As System.IO.FileInfo In DI.GetFiles
            DR = DT.NewRow
            DR("FileName") = "Upload/thumbs/" & FI.Name
            DT.Rows.Add(DR)
        Next
        Repeater1.DataSource = DT
        Repeater1.DataBind()
        DT = Nothing
        DR = Nothing
        DI = Nothing
    End Sub

    Protected Sub FileUploader1_FileReceived(ByVal sender As Object, ByVal e As com.flajaxian.FileReceivedEventArgs)
        Repeater1.DataBind()
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body style="background-color:silver">
    <form id="form1" runat="server">
    <h2>File Uploader</h2>
<p>This will let users upload files to server and create thumbnails of the orginal file.
<br />Size Limit is 100KB for each image.
<br />Once the files have been uploaded, you will see the thumbnail of the orginal image.
<br />Click on the image to open the thumbnail image
<br />This will also create folder Upload and the sub folder Thumbs (the thumbnail images will be stored there).
<br />and the images will be placed there.</p>
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Repeater ID="Repeater1" runat="server" OnLoad="Repeater1_Load">
                    <ItemTemplate>
                        <a href=' <%# Eval("FileName") %>' target="_blank"><img alt=' <%# Eval("FileName") %>' border="0" src=' <%# Eval("FileName") %>' style="border: thin solid #000000; margin: 5px"></a>
                    </ItemTemplate>
                </asp:Repeater>
                <br />
                <cc1:FileUploader ID="FileUploader1" runat="server"
                    AllowedFileTypes="Web Images (Jpeg, Gif, Png):*.jpg;*.jpeg;*.png;*.gif"                   
                    MaxNumberFiles="1"
                    MaxFileNumberReachedMessage="Maxium file that can be sent is {0}"
                    MaxFileSize="100KB"
                    MaxFileSizeReachedMessage="No files bigger than {0} are allowed"
                    JsFunc_FileStateChanged="FileStateChanged"
                    UseInsideUpdatePanel="true"  
                    TransparentBackground="true"
                    OnFileReceived="FileUploader1_FileReceived">
                    <Adapters>
                        <cc1:FileSaverAdapter FolderName="Upload" />
                        <cc1:ThumbGeneratorAdapter FolderName="upload/thumbs" MaximumHeight="100" MaximumWidth="100" Suffix="_s" />
                    </Adapters>
                </cc1:FileUploader>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

[CODE END]

I hope this helps
Feb 7, 2009 at 6:35 PM
This is certainly a big help!  Question for you though, I'm not sure if you've run into it or not but I'm working with a querystring to determine what folder I should put the files in.  I'm also giving the files custom names.  I've noticed though that when the uploader is posting the files, it seems to clear the querystrings I'm using to set these folders.  Have you had any experience with this?

Thanks!
Coordinator
Feb 7, 2009 at 11:24 PM
Guys, you don't read the help file.

If you want to pass variables with the request use State container as shown here:

Feb 8, 2009 at 3:26 AM
Hi, I did read the help file... I guess its not laymen for me. But looking here and  trial and error, I seem to get it done. Not to say this isn't a great thing you have here. I mostly code in VB and most of the code here is CS. Have to try to convert it. That is why I place this sample page here so others can pick it apart and add things to it.

I see others asking questions about this  and they are mostly sent to the help file, but I think if there was usage examples it may get the point across.

Like I have said before... this is a great package and love to learn more on how to use it with databases (Access and SQL).

Coordinator
Feb 8, 2009 at 5:45 AM
Edited Feb 8, 2009 at 5:45 AM

Well in the help file almost all is markup and JavaScript so the C# shouldn't be a big issue there. Pretty much just remove the semicolon at the end. 

But I really appreciate that you want to share with others your code. We can possibly think something about submitting codesnippets including in VB I think, if that helps…

Coordinator
Feb 8, 2009 at 6:01 AM
Edited Feb 8, 2009 at 6:05 AM
I have created a google group http://groups.google.com/group/code-snippets-flajaxianfileuploader/ where everyone could post a code snippet. Let's see if the idea takes off we can add a link to that on the main page.
Feb 8, 2009 at 6:17 AM
That would be a great idea, I will post anything I have here. I hope this takes off and other users will share too.

Thanks flajaxian