Guest User

WiseJ Upload Control sample

a guest
Jan 5th, 2020
307
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ''' <summary>
  2.    ''' Attach handler to Upload control. So a click on the control executes Javascript to invoke the wiseJ upload mechanism
  3.    ''' </summary>
  4.    ''' <param name="ctrl"></param>
  5.    Public Sub AttachUploadInvokeToControl(ByVal ctrl As Control)
  6.         Me.Upload1.CreateControl()
  7.         ctrl.Eval(String.Format(vbCr & vbLf & "this.uploadButton = Wisej.Core.getComponent(""id_{0}"");" & vbCr & vbLf &
  8.                       " this.addListener(""execute"", function(e)" & vbCr & vbLf &
  9.                       "  {{" & vbCr & vbLf & "                 this.uploadButton.upload();" & vbCr & vbLf &
  10.                       " }});", Me.Upload1.Handle))
  11.  
  12.     End Sub
  13.  
  14.  
  15.  
  16.  
  17.     ''Event for upload progress. Update UI based on upload progress
  18.    Private Sub Upload1_Progress(sender As Object, e As UploadProgressEventArgs) Handles Upload1.Progress
  19.         Dim intLoaded As Integer = e.Loaded
  20.         Dim intTotal As Integer = e.Total
  21.  
  22.         Dim sngPercent As Single = intLoaded / intTotal
  23.         Dim strTotalPercent = String.Format("{0:0%}", sngPercent)
  24.         If strTotalPercent <> "NaN" Then
  25.             Me.lblTapForPhoto.Text = "Tap to add an optional photo. (" + strTotalPercent + ")"
  26.         End If
  27.     End Sub
  28.  
  29.  
  30.     Private Sub Upload1_Uploaded(sender As Object, e As Wisej.Web.UploadedEventArgs) Handles Upload1.Uploaded
  31.         LoadFile(e.Files)
  32.     End Sub
  33.  
  34.  
  35.     ''' <summary>
  36.    ''' Loads an uploaded file into the picturebox, saves file on server temporarily
  37.    ''' </summary>
  38.    ''' <param name="files"></param>
  39.    Private Sub LoadFile(files As HttpFileCollection)
  40.         If files Is Nothing Then
  41.             Return
  42.         End If
  43.  
  44.         If files.Count = 0 Then
  45.             'If no file was uploaded, reset picturebox to nothing..
  46.            Me.pbPicture.Image = Nothing
  47.         Else
  48.             'If there is a file, set the picturebox to it
  49.            Dim CIR As New CustomImageResizer(files(0).InputStream)
  50.             Me.pbPicture.Image = CIR.CropImage(pbPicture.Width, pbPicture.Height)
  51.             CIR.DisposeOriginalBMP()
  52.  
  53.  
  54.             If Me.pbPicture.Image Is Nothing Then
  55.                 AlertBox.Show("Image file could not be decoded. Please try again", MessageBoxIcon.Error)
  56.                 lblTapForPhoto.Text = "Tap to add an optional photo."
  57.                 Me.pbPicture.ImageSource = "Resources/Images/noimage.png"
  58.                 boolImageSuccessfullyUploaded = False
  59.                 Exit Sub
  60.             End If
  61.             strImageTemporaryFileName = files(0).FileName
  62.  
  63.             'Save image to server temporarily
  64.            If files(0).ContentLength > 0 Then
  65.                 Dim temp As String = System.IO.Path.GetTempPath()
  66.                 Dim path__1 = System.IO.Path.Combine(temp, strImageTemporaryFileName)
  67.                 files(0).SaveAs(path__1)
  68.                 strImageTemporaryFileName = path__1
  69.             End If
  70.  
  71.             'Me.pbPicture.Image.Width'Perform any image resizing here
  72.  
  73.             boolImageSuccessfullyUploaded = True 'set success flag.
  74.        End If
  75.     End Sub
RAW Paste Data