Click here to Skip to main content
15,944,401 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm trying to get the 'Uploadify' upload progress plugin working with an existing asp/ site that uploads pictures to a database.

The existing site uses an asp.fileupload component and the upload action is via a separate upload button. All code for this is in the asp forms aspx.vb file.

Ok, I've got the gui side of things sorted but I'm not sure how to get uploadify to fire the existing btnUpload_Click event.
Presumably teh problem is with directing the 'uploader' variable. I see a lot of people utilising generic handler ashx routines but I have no idea how to move my btnUpload code to this and retain all the form input info.
Can someone advise me on how to deal with this problem?

I have this code in the pages HEAD section:
<link href="~/Styles/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src="Uploadify/jquery.uploadify.min.js"></script>

<script type="text/javascript">
$(function() {
    $("#<%=fupImage.ClientID %>").uploadify({
        'swf'       : 'Uploadify/uploadify.swf',
        'uploader'  : 'uploadify.php',
        'buttonText': 'Select Image',
        'cancelImg' : 'images/uploadify-cancel.png',
        'folder'    : 'uploads',
        'fileDesc'  : 'Image Files',
        'fileExt'  : 'Image Files (.JPG, .JPEG, .GIF, .BMP, .PNG)',
        'sizeLimit' : 4000000,
        'auto'      : false,
        'multi'     : false

and here's my btnUpload code:
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
    pnlConfirmation.Visible = False
    lblUploadResult.Text = ""
    lblUploadResult.ForeColor = Color.DarkGreen

    'validate all fields before uploading
    If Not validateControls() Then
        lblUploadResult.Text += vbCrLf + "Try again."
        lblUploadResult.ForeColor = Color.Red
        pnlConfirmation.Visible = True
        Exit Sub
    End If

    'check for any other entries by this person
    If checkOtherEntries() Then
        lblUploadResult.Text = "There is already an entry for this email address." + vbCrLf + _
                               "If you feel this is an error then please email" + vbCrLf + _
        lblUploadResult.ForeColor = Color.Red
        pnlConfirmation.Visible = True
        Exit Sub
    End If

    '**All validated so allow the upload

    'check to make sure a file is selected before adding it to the db
    If fupImage.PostedFile IsNot Nothing AndAlso fupImage.HasFile AndAlso Not String.IsNullOrEmpty(fupImage.PostedFile.FileName) AndAlso CustomValidatorFormat.IsValid Then

        'check for Tsan connection. If none then save locally
        Dim tsanFolder As String = ConfigurationManager.AppSettings("TsanLocalFolder")
        If (Not Directory.Exists(tsanFolder)) Then
        End If
        Dim thumbsFolder As String = ConfigurationManager.AppSettings("MailThumbsFolder")
        If (Not Directory.Exists(thumbsFolder)) Then
        End If

        'store the currently selected file in memeory
        Dim img As HttpPostedFile = fupImage.PostedFile

        'Make sure a duplicate file doesn’t exist on Tsan.  If it does, keep on appending an incremental numeric until filename is unique
        Dim sFilename As String = System.IO.Path.GetFileName(img.FileName)
        'check if filename exists on Tsan - increment name if it does
        Dim newFileName As String = getTSANfilename(tsanFolder, sFilename)
        'save to TsanFolder- full res
        '  fupImage.PostedFile.SaveAs(sFolder & sFilename)
        img.SaveAs(tsanFolder & newFileName)

        'make a thumb and save to dB
        Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(fupImage.PostedFile.InputStream)
        Dim imageHeight As Integer = imageToBeResized.Height
        Dim imageWidth As Integer = imageToBeResized.Width
        Dim maxWidth As Integer = 300
        Dim iDivisor As Integer = imageWidth / maxWidth
        imageHeight = imageHeight / iDivisor
        imageWidth = maxWidth

        Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
        Dim stream As System.IO.MemoryStream = New MemoryStream()
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
        stream.Position = 0
        Dim image As Byte() = New Byte(Stream.Length) {}
        stream.Read(image, 0, image.Length)

        'connect to the db
        Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("dBconnString").ConnectionString)

        'sql command to save our image data to the db
        Dim cmd As New SqlCommand("INSERT INTO EntryData(imgBin, imgFileNameSubmitted, imgFileNameSaved, imgSize, imgType, imgDate, imgDescription, NameFirst, NameLast, Email, Phone, Address_Unit, Address_Number, Address_Street1, Address_Street2, Address_Suburb, Address_State, Address_PostCode, Address_Country, Referral, imgTitle) " & _
                                  "VALUES (@imgBin, @imgFileNameSubmitted, @imgFileNameSaved, @imgSize, @imgType, @imgDate, @imgDescription, @NameFirst, @NameLast, @Email, @Phone, @Address_Unit, @Address_Number, @Address_Street1, @Address_Street2, @Address_Suburb, @Address_State, @Address_PostCode, @Address_Country, @Referral, @imgTitle) " & _
                                  "SELECT @@IDENTITY", conn)
        cmd.CommandType = CommandType.Text

        'add thumbnail img binary data
        ' cmd.Parameters.Add("@imgBin", SqlDbType.Image, imgBin.Length).Value = imgBin
        cmd.Parameters.Add("@imgBin", SqlDbType.Image, image.Length).Value = image
        'add submitted img file name
        cmd.Parameters.AddWithValue("@imgFileNameSubmitted", sFilename)
        'add saved img file name
        cmd.Parameters.AddWithValue("@imgFileNameSaved", newFileName)
        'add img size
        cmd.Parameters.AddWithValue("@imgSize", img.ContentLength) 'imgBin.Length)
        'add img type
        cmd.Parameters.AddWithValue("@imgType", img.ContentType)
        'add img date
        cmd.Parameters.AddWithValue("@imgDate", DateTime.Parse(Now.Date()))
        'add img message
        cmd.Parameters.AddWithValue("@imgDescription", txtDescription.Text)
        'add img title
        cmd.Parameters.AddWithValue("@imgTitle", txtTitle.Text)
        'add first name
        cmd.Parameters.AddWithValue("@NameFirst", txtFirstName.Text)
        'add last name
        cmd.Parameters.AddWithValue("@NameLast", txtLastName.Text)
        'add email
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text)
        'add phone number
        cmd.Parameters.AddWithValue("@Phone", txtPhone.Text)
        'add unit number
        cmd.Parameters.AddWithValue("@Address_Unit", txtUnit.Text)
        'add street number
        cmd.Parameters.AddWithValue("@Address_Number", txtNumber.Text)
        'add street 1
        cmd.Parameters.AddWithValue("@Address_Street1", txtStreet.Text)
        'add street 2
        cmd.Parameters.AddWithValue("@Address_Street2", txtStreet2.Text)
        'add Suburb
        cmd.Parameters.AddWithValue("@Address_Suburb", txtSuburb.Text)
        'add state
        cmd.Parameters.AddWithValue("@Address_State", ddlState.Text)
        'add post code
        cmd.Parameters.AddWithValue("@Address_PostCode", txtPostCode.Text)
        'add country
        cmd.Parameters.AddWithValue("@Address_Country", "Australia")
        'add referral info
        If RadioButton1.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton1.Text)
        ElseIf RadioButton2.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton2.Text)
        ElseIf RadioButton3.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton3.Text)
        ElseIf RadioButton4.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton4.Text)
        ElseIf RadioButton5.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton5.Text)
        ElseIf RadioButton6.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton6.Text)
        ElseIf RadioButton7.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton7.Text)
        ElseIf RadioButton8.Checked Then
            cmd.Parameters.AddWithValue("@Referral", RadioButton8.Text)
        End If

        Dim id As Integer
        Using conn
            'open the connection
            'send the sql query to store the data
                id = Convert.ToInt32(cmd.ExecuteScalar())
                ' lblUploadResult.Text = String.Format("Picture ID is {0}", id)
                lblUploadResult.Text = "Photo submitted.<br />Thank you for your entry"
                ' Display the image from the tsan
                Image1.ImageUrl = "~/ShowImage.ashx?id=" & id
                Image1.Visible = True
            Catch ex As Exception
                Dim str As String = ex.Message
                lblUploadResult.ForeColor = Color.Red
                lblUploadResult.Text = "There was an upload error.<br />" + str
            End Try
        End Using

        'Display the result of the upload.
        pnlConfirmation.Visible = True

        'send some notification emails
       Dim sArgs As String = id.ToString + "," + sFilename + "," + newFileName + "," + txtFirstName.Text + "," + txtLastName.Text + "," + txtUnit.Text + "," + txtNumber.Text + "," + _
                              txtStreet.Text + "," + txtStreet2.Text + "," + txtSuburb.Text + "," + ddlState.Text + "," + txtPostCode.Text + "," + "Australia" + "," + txtPhone.Text + "," + _
                              txtEmail.Text + "," + txtTitle.Text + "," + tsanFolder + "," + thumbsFolder
        Dim w As New Work()
        w.Data = sArgs
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread = New Thread(AddressOf w.DoMoreWork)
    End If
End Sub
Updated 1-Aug-13 11:48am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900