Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting the size of an uploaded file 1

Status
Not open for further replies.

Blueie

Technical User
May 12, 2012
72
GB
Hello

I have the following script to upload files:

Code:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
                Label1.Text = "File name: " & _
                     FileUpload1.PostedFile.FileName & "<br>" & _
                     "File Size: " & _
                     FileUpload1.PostedFile.ContentLength & "<br>" & _
                     "Content type: " & _
                     FileUpload1.PostedFile.ContentType & "<br>" & _
                     "Location Saved: C:\Uploads\" & _
                    FileUpload1.FileName
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else : Label1.Text = "You have not specified a file."
        End If

        Label1.Attributes.Add("style", "display:block")
    End Sub

It seems to work OK. I also aim to limit the size of files that can be uploaded and use this Web1.config file (so I have two Web.config files: the other one has my database connection details in it):

Code:
<?xml version="1.0"?>

<configuration>

  <system.web>
    <compilation debug="false" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>


  <authentication mode="Forms">

    <authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>

    <forms loginUrl="upload.aspx"
         protection="All"
         timeout="30"
         name="AppNameCookie"
         path="/FormsAuth"
         requireSSL="false"
         slidingExpiration="true"
         defaultUrl="index.aspx"
         cookieless="UseCookies"
         enableCrossAppRedirects="false"/>
</authentication>

  <!--Limit file size to 500KB-->

  <system.web>
    <httpRuntime maxRequestLength="524288"/>
  </system.web>

</configuration>

Is my upload.aspx or upload.aspx.vb file able to 'see' this Web.config file because I haven't referenced it in either of those two aspx files?

I wonder also if it may be worth notifying the user if he tries to go over the file size limit, but I am not sure how to do that.

Many thanks!



 
Many thanks for that, jbenson001

There are lots of links to other links there, too. Lots of reading to get through [bigsmile]

Blueie
 
Glad to help

Shouldn't be too bad, lots of the links point to the same thing so it should be a short read.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top