Iron Speed Technical Forums
Register Latest Topics
 
 
 


Reply
  Author   Comment   Page 1 of 2      1   2   Next
snow

Registered:
Posts: 19
Reply with quote  #1 
Hi, I've followed the video demo on IronSpeed website to add file upload, which worked great. The data type for upload field is "Image". It allows any type of file to be uploaded though. Just wondering if there is a way to limit the file format, say just pdfs?

Thanks in advance.

__________________
Present is a gift.
akeshgupta

Avatar / Picture

Iron Speed MVP
Design, Develop & Deliver

Registered:
Posts: 1,095
Reply with quote  #2 
You can always check the extension of the uploaded file to see if it ends with PDF and if not, raise a validation error.  You can google for finding the filename of the uploaded control.

HTH,

__________________
Akesh Gupta
Light Speed Solutions, LLC.
If I rest, I will rust !   Let's share the knowledge !
TrucMi

Registered:
Posts: 220
Reply with quote  #3 

For Add/Edit page you can add customized code like shown below to control the file types.  On your Add/Edit page locate the Save button and add the modified codes below.  You will need a Label somewhere on the page to show the error message if users selected wrong file type, I called my control as ErrorMsgLabel and set it's Text to blank by default.

To control the file size you will need to locate the line of code below in the web.config and adjust the maxRequestLength to the file size allowed.

<httpRuntime maxRequestLength="10240" requestValidationMode="2.0" enableVersionHeader="false" />


Note: This method is only for Add/Edit page. I don't know yet how to do this in the Table records.  Good luck!

Public Sub SaveButton_Click(ByVal sender As Object, ByVal args As EventArgs)
          ' Click handler for SaveButton.
          ' Customize by adding code before the call or replace the call to the Base function with your own code.
          'SaveButton_Click_Base(sender, args)
          ' NOTE: If the Base function redirects to another page, any code here will not be executed.
  
  '*** Customized Code: Filter what File format and File size user can upload into the Database.
  If Not Doc.HasFile Then
   'SaveButton_Click_Base(sender, args)
   'Display error message because user did not select a file to Upload
   ErrorMsgLabel.Text = "* ERROR! You must select a File to upload."
  Else

  If Doc.HasFile Then
   Dim filepath As String = Doc.PostedFile.FileName
   Dim fileExt As String = LCase(System.IO.Path.GetExtension(Doc.FileName))
   Dim myUpload As HttpPostedFile = Doc.PostedFile

   'Control file-types and Size: Allow only Excel, Word and PDF format and the size must be under 10 MB.
   If (fileExt = ".xls" Or fileExt = ".xlsx" Or fileExt = ".doc" Or fileExt = ".docx" Or fileExt = ".pdf") And Doc.PostedFile.ContentLength < 10000000 Then
    Try
     SaveButton_Click_Base(sender, args)
    Catch ex As Exception
     ErrorMsgLabel.Text = "ERROR: " & ex.Message.ToString() & ""
    End Try
   Else
    'Display error message
    ErrorMsgLabel.Text = "* ERROR! Only Excel, Word or PDF files are allowed! Also, File must be less than 10 MB."
 
   End If

  End If

  End If
  
        End Sub

snow

Registered:
Posts: 19
Reply with quote  #4 
Thank you Akesh for pointing to a good direction.

TrucMi, you are awesome! I followed your line of logic. It's now working like charm.

Instead of doing the validation in the Save button click event though, I customized the file upload control's validate() method.

***

 Public Overrides Sub Validate()
            ' Add custom validation for any control within this panel.
            ' Example.  If you have a State ASP:Textbox control
            ' If Me.State.Text <> "CA" Then
            '    Throw New Exception("State must be CA (California).")
            ' End If

            ' The Validate method is common across all controls within
            ' this panel so you can validate multiple fields, but report
            ' one error message.

            MyBase.Validate()
       
            'Validate the file format and size
            If Not Me.DocFile.PostedFile Is Nothing Then
                If Me.DocFile.PostedFile.FileName.Length > 0 AndAlso Me.DocFile.PostedFile.ContentLength > 0 Then
                    Dim filepath As String = Me.DocFile.PostedFile.FileName
                    Dim fileExt As String = LCase(System.IO.Path.GetExtension(filepath))

                    'Control file-types and Size: Allow only Word and PDF format and the size must be under 10 MB.
                    If (fileExt = ".docx" Or fileExt = ".pdf") And Me.DocFile.PostedFile.ContentLength <= 10000000 Then
                        'Do nothing, File is validated
                    Else
                        'raise validation error
                        Throw New Exception("ERROR: You can only upload pdf files that are less than 10MB in size.")
                    End If

                End If
            End If
   End Sub

__________________
Present is a gift.
TrucMi

Registered:
Posts: 220
Reply with quote  #5 
Great! happy it works for you and thanks for mentioning the validate() control.  I will try this option too.  I've learn new things everyday through sharing. :-)

ehsan.haque

Registered:
Posts: 14
Reply with quote  #6 
As in the second piece of code submitted by TrucMi, can someone please tell me where the "file upload control's validate() method." is? or what does it exactly mean.  I do not seem to locate this to put the code in. I need to filter upload files to restrict it to certain types on a tableControl.

Thanks
Ehsan

__________________
Ehsan Haque
JimiJ

Avatar / Picture

MVP Developer
Registered:
Posts: 1,950
Reply with quote  #7 
Hi Eshan,

The Validate() method has always nothing it it. You may search "void Validate()" to find them in every TableControl class. 

To use one, you need to override it in the Page class and write your own code or copy part of TrucMi's code to meet your needs.


Cheers,
Jimi J

__________________

  Jaime Jegonia
[jts_logo]

Iron Speed MVP Developer
 

". . . and whoever sows generously will also reap generously" 2 Cor 9:6

ehsan.haque

Registered:
Posts: 14
Reply with quote  #8 
Hi Jimi,

I am not that good at coding. I tried the way you said but getting some weird errors. Do you have anything that I could copy and paste and then alter to make it work?

What I basically need is, I have a table control with a field called "UploadDoc" defined as Type Binary. I just need only certain files can be uploaded eg, .docx, .pdf etc.

Attaching the table control file. 

Thanks.





 
Attached Files
doc ApplicantUpload.doc (472.00 KB, 6 views)


__________________
Ehsan Haque

vmnz

Registered:
Posts: 34
Reply with quote  #9 
Sorry to interfere, but for new browsers with html5 support you can use ACCEPT attribute in FileUpload control (less coding required as well).
I.e.:
Code:

<asp:fileupload cssclass="field_input" id="FileUpload" runat="server" accept=".docx,.pdf"></asp:fileupload>


Or alternatively, do same thing using regex validator, something like:
Code:

<asp:fileupload cssclass="field_input" id="FileUpload" runat="server"/>
<asp:RegularExpressionValidator ID="regexValidator" runat="server" ControlToValidate="FileUpload" ErrorMessage="Only files of .docx type can be uploaded" ValidationExpression="(.*\.([dD][oO][cC])$)"></asp:RegularExpressionValidator>
ehsan.haque

Registered:
Posts: 14
Reply with quote  #10 
Hi VMNZ,

This looks much easier for me. I am mostly familiar working with the IDE and do not have a lot of experience with the coding side of it. Would you be able to tell me where exactly I can put the code (html5 ACCEPT attribute) in. If you have an example I would greatly appreciate if could send that to me. Once again thanks for your feedback on this.

Ehsan

__________________
Ehsan Haque
vmnz

Registered:
Posts: 34
Reply with quote  #11 
Drag the File Upload control into desired cell in your Design Mode window, then add accept attribute to it, something like that:
Capture.PNG

ehsan.haque

Registered:
Posts: 14
Reply with quote  #12 
HI VMNZ,

Thanks so much for your help. I just have a small issue as I am trying to implement this on an existing page I have a Code generation Upload tag not a ASPX upload control. Not sure how to implement this on a GEN Tag.
File upload Gen.JPG 


__________________
Ehsan Haque
vmnz

Registered:
Posts: 34
Reply with quote  #13 
Mate, its not a problem! In your UploadDoc control's Properties click on Custom properties and add new property accept with value .docx,.pdf

That would give you same result.
JimiJ

Avatar / Picture

MVP Developer
Registered:
Posts: 1,950
Reply with quote  #14 
Hi vmnz,

What will happen if you try to upload other file types? Will it be rejected? Will the user receives a message?


Cheers,
Jimi J

__________________

  Jaime Jegonia
[jts_logo]

Iron Speed MVP Developer
 

". . . and whoever sows generously will also reap generously" 2 Cor 9:6

vmnz

Registered:
Posts: 34
Reply with quote  #15 
Hi Jaime

I agree, users can override that "accept" method by selecting "all files" in the select file dialog.

However, in order to prevent undesirable file types been uploaded, RegularExpressionValidator can be used along (as it was mentioned in my first post here).

Capture.PNG 


Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.

Download Iron Speed Designer

Terms of Service Privacy Statement