• The eternal confessions of a beautiful mind...
  • DamianM.Co.UK
  • Home
  • About
  • Archives
  • Contact
  • Sitemap
  • My Flickr

    IMG_9585IMG_9115IMG_9113IMG_9111IMG_9078IMG_9075IMG_9069IMG_9065IMG_9041IMG_9032IMG_8963IMG_8928IMG_8916IMG_8915IMG_8904IMG_8876IMG_8858IMG_8830IMG_8828IMG_8826

  • Recent Posts

    • Can you spot the bands?
    • Strictly for the office
    • Failing the exam with dignity
    • New Ohio Roller Coaster - INSANE!!!
    • Speeding
    • Captions not required
    • Unfortunate Backgrounds
    • Unfortunate Signs
    • OMG!!
    • Women as explained by engineer
    • The Monty Hall Problem
    • ahhh still love them - Motivational Posters
    • What does Mona Lisa do when the Museum is closed………
    • 21st Century kids books
    • Cool Origami
  • My Tools

    • Blog_LinkIt
    • DCoda Theme
    • DCoda Widgets
    • RSS_Sticky
    • WordPress.org
    • WP_BlogNetworking
    • WP_BlogRollSync
    • WP_BoilerPlate
    • WP_Censor
    • WP_ContactMe
    • WP_DeliciousPost
    • WP_EasyReply
    • WP_HeadNFoot
    • WP_LinkIt
    • WP_OneInstall
    • WP_PostDate
    • WP_PostNotes
    • WP_RssSticky
    • WP_Spoiler
    • WP_Submission
  • My Web

    • ASPAlliance
    • ClaimID
    • del.ico.us
    • Digg
    • DSLRBlog
    • DVDProfiler
    • Flickr
    • Honeyed SPAM
    • My Blog
    • My company
    • MYSpace
    • WordPress.org
    • YouTube
    « Top 5 Recut Trailers
    Suspect until proven guilty »

    File uploading with .NET

    One of the new additions in .NET is the ability to upload files without the need for additional components. The class HttpPostedFile deals with posted files. It has only a few members, but these cover anything you would want to do with and upload. Here is an example that hopefully demonstrates everything you need to know.

    Upload Source Code

    The one thing that you must remember when uploading files, is to set the encoding type. If you do not, it will not upload the file.

    2
    
    <form id="Form1" method="post" runat="server" enctype="multipart/form-data" action="/code/2/upload.aspx">

    So how easy is it to up load the file? Very easy, it can be done in just one line. In the supplied example, the class is copied into a variable for clarity, as it will be referred to numerous times, so it takes three lines.

    11
    
            Dim File As HttpPostedFile
    21
    
                File = Request.Files.Get(0)
    30
    
                        File.SaveAs(Server.MapPath("....filesuploaddeleteme.txt"))

    Note: The user ASPNET must have write access to the target directory if you wish to save the file.

    A nice feature of the class if that the files size and type are available without having to save the file to disk. In the example, this is used to stop large files and files that cannot be displayed in the page.

    23
    24
    25
    26
    27
    28
    29
    30
    31
    
                If File.ContentLength &gt; 1024 Then
     
                    ErrorMsg.Text = "File is bigger than 1k"
     
                Else
     
                    If File.ContentType &lt;&gt; "text/plain" Then
     
                        ErrorMsg.Text = "File is not a text file"

    The ContentType is set using the files MIME type, not the extension and recognizes most popular file formats.

    So, all you want to do is process the file for the user or put it into a database, why bother saving it if you do not have to. The class gives you access to the upload stream which can be read just like any stream.

    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
                        Dim sr As System.IO.StreamReader
     
                        Dim strContent As String
     
                        sr = New System.IO.StreamReader(File.InputStream)
     
                        'display contents
     
                        FileContents.Text = sr.ReadToEnd
     
                        sr.Close()

    Note: If you wish to access the uploaded file as a stream and also want to save it to disk, save it first, as there is a slight lag when closing the stream which may cause the saved file to be empty.

    This entry was posted on Thursday, June 14th, 2007 at 7:21 pm and is filed under ASP.NET, Coding. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.