How do I get UploadAttachments events to fire for server-side validation prior to upload?

Last post 03-13-2012, 2:18 PM by Adam. 1 replies.
Sort Posts: Previous Next
  •  02-24-2012, 3:24 PM 73167

    How do I get UploadAttachments events to fire for server-side validation prior to upload?

    Using the standard .net upload control, I can do this:
     
        Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
     
              Dim fname As String = FileUpload1.FileName
     
              'Here I can do some validation on the file type
               If fname.EndsWith(".jpg") Then
              
                       'I can also augment the file name prior to uploading, like this:
                       fname = Now.ToString("yyyyMMdd-HHMMss-") & fname
     
                      'After validation and file name augmentation, I can then choose to upload the file:
                      FileUpload1.SaveAs(Server.MapPath("~/files/") & fname
     
             Else
                      'Inform user that file must be of type .jpg
     
             End If
     
        End Sub
     
     
    I just want to duplicate this simple behavior with the Ajax Uploader.  Is this possible? 
     
    In the example above, I can do the validation BEFORE the upload ever occurs.  I tried to do this with the Ajax Uploader but when calling UploadAttachments1.Upload manually, I'm required to send the file name, file size, and temp path, which I can't seem to get from the control.  I learned in another post how to retrieve the file name, size, path AFTER upload or via Javascript prior to upload, but neither of these options give me the ability to do simple server-side validation as in the example above.  I see several events such as AttachmentAdded, AttachmentClicked, AttachmentDataBound, etc. but I am unable to get any of these to fire (except for AFTER the file has been uploaded).
     
    I seem to be running in circles.
     
    Is there any way to duplicate the simplicity of the code above with the Ajax Uploader?  
     
    Thank you.
  •  03-13-2012, 2:18 PM 73430 in reply to 73167

    Re: How do I get UploadAttachments events to fire for server-side validation prior to upload?

    You can do the same thing in Ajax Uploader.
     
     
    Private Sub Uploader_FileUploaded(ByVal sender As Object, ByVal args As UploaderEventArgs)
                
                Dim fname As String = args.FileName
     
                'Here I can do some validation on the file type
               If fname.EndsWith(".jpg") Then
              
                       'I can also augment the file name prior to uploading, like this:
                       fname = Now.ToString("yyyyMMdd-HHMMss-") & fname
     
                      'Copies the uploaded file to a new location.
                      'args.CopyTo("c:\\temp\\"& fname) 
             Else
                      'Inform user that file must be of type .jpg 
             End If 
    End Sub 
    <CuteWebUI:Uploader runat="server" ID="Uploader1" OnFileUploaded="Uploader_FileUploaded"></CuteWebUI:Uploader>
     
    Even better, Ajax Uploader can detect the allowed file upload types in the client end.
     
    Demo: 
     
     
    Code:
     
    <CuteWebUI:Uploader runat="server" ID="Uploader1" OnFileUploaded="Uploader_FileUploaded">
          <ValidateOption AllowedFileExtensions="jpeg,jpg,gif,png" MaxSizeKB="100" />
    </CuteWebUI:Uploader>
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML