SharePoint / ASP.Net Development
Experiences from the field...
Navigation
   RSS 2.0
Categories
Entries by Month

# Tuesday, July 12, 2005

In response to a post in the ASP.Net forums, here is a routine to upload a file to a document library.

Sub ProcessPostedFile(ByVal fileUpload As HtmlControls.HtmlInputFile)
    Try
    ' get the filename and stream
    Dim fn As String = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName)
    Dim stm As System.IO.Stream = fileUpload.PostedFile.InputStream
    Dim contents(CInt(stm.Length)) As Byte

    stm.Read(contents, 0, CInt(stm.Length))
    stm.Close()

    ' get the library path
    Dim docLibPath As String = Configuration.ConfigurationSettings.AppSettings("DocUploadPath")
    ' first, get the site containing the library
    Dim site As SPSite = New SPSite(docLibPath)
    Dim web As SPWeb = site.OpenWeb
    ' then get the folder
    Dim docFolder As SPFolder = web.GetFolder(docLibPath)

    ' delete the file if it exists
    Dim docFile As SPFile
    Try
        docFile = docFolder.Files(fn)
    Catch ex As Exception
    End Try

    If Not docFile Is Nothing Then
        docFolder.Files.Delete(fn)
    End If

    ' save the file
    docFile = docFolder.Files.Add(fn, contents)

    Catch ex As Exception
        Context.Trace.Warn(ex.ToString)
    End Try
End Sub

Tuesday, July 12, 2005 12:43:24 PM (Central Daylight Time, UTC-05:00)  #    Comments [1]  |  Tracked by:
"Programmatically upload a file to a document library" (SharePointKicks.com) [Trackback]

Search

Further Reading...

Powered by: newtelligence dasBlog 2.2.8279.16125

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008, Paul Schaeflein

Send mail to the author(s) E-mail