Paul Schaeflein did a post about an example how to programmatically upload a file to a document library. The example is in VB.NET and could be very handy to use in custom made WebParts. Thanks for that! :)
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
http://sharepointblogs.com/SPDiary/archive/2005/07/12/2632.aspx
Article written by Bil Simser doing the same upload using a HTTP PUT. Meaning that you could use it from any place and not only on the SharePoint server.
http://weblogs.asp.net/bsimser/archive/2004/06/06/149673.aspx