CSDN博客

img lanbaibai

文件上传下载webservice

发表于2004/8/18 9:53:00  1697人阅读

分类: .net

Imports System.Web.Services
Imports System.Configuration
Imports System.IO


<WebService(Namespace:="http://www.dynamics-apps.com")> _
Public Class FileServer
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call
    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    Private Shared WebServerPath As String = ConfigurationSettings.AppSettings("WebServerRootPath")

    Public Enum FileExtensions
        htm
        html
        asp
        aspx
        jpg
        gif
        dll
        exe
        all
    End Enum

    Public Class FileInformation
        Public Name As String
        Public Size As Long
        Public CreadedDate As DateTime
        Public LastModified As DateTime
        Public LastAccess As DateTime
        Public FileType As String
        Public FileLocation As String
        Public FileContent As Byte()
    End Class

    <WebMethod(Description:="Retrieve a boolean indicating if Path exists or not in WebServer")> _
    Public Function DirectoryExists(ByVal Path As String) As Boolean
        Dim di As New DirectoryInfo(WebServerPath & Path)
        Return di.Exists
    End Function

    <WebMethod(Description:="Retrieve a boolean indicating if File exists or not in WebServer")> _
    Public Function FileExists(ByVal Path As String, ByVal FileName As String) As Boolean
        Dim di As DirectoryInfo
        Dim fi As FileInfo
        Try
            di = New DirectoryInfo(WebServerPath & Path)
            If Not di.Exists Then
                Return False
                Exit Function
            End If
            Dim afi As FileInfo() = di.GetFiles()
            For Each fi In afi
                If fi.Name = FileName Then
                    Return True
                    Exit For
                End If
            Next
        Catch
            Return False
        End Try

    End Function

    <WebMethod(Description:="Retrieve an array of files with name, attributes and content.")> _
    Public Function Browse(ByVal VirtualPath As String, ByVal FileExtension As FileExtensions) As FileInformation()
        Dim i As Integer
        Dim fi As FileInfo
        Dim aFiles As FileInformation()
        Dim mExtension As String
        Select Case FileExtension
            Case FileExtensions.asp
                mExtension = "asp"
            Case FileExtensions.aspx
                mExtension = "aspx"
            Case FileExtensions.gif
                mExtension = "gif"
            Case FileExtensions.htm
                mExtension = "htm"
            Case FileExtensions.html
                mExtension = "html"
            Case FileExtensions.jpg
                mExtension = "jpg"
            Case FileExtensions.dll
                mExtension = "dll"
            Case FileExtensions.exe
                mExtension = "exe"
            Case FileExtensions.all
                mExtension = "*"
        End Select
        Dim di As New DirectoryInfo(WebServerPath & VirtualPath)
        Dim afi As FileInfo() = _
         di.GetFiles("*." & mExtension)
        ReDim Preserve aFiles(afi.Length - 1)
        For Each fi In afi
            aFiles(i) = New FileInformation()
            aFiles(i).Name = fi.Name
            aFiles(i).Size = fi.Length
            aFiles(i).LastAccess = fi.LastAccessTime
            aFiles(i).CreadedDate = fi.CreationTime
            aFiles(i).LastModified = fi.LastWriteTime
            aFiles(i).FileType = fi.Extension
            aFiles(i).FileLocation = fi.DirectoryName
            aFiles(i).FileContent = ReadFile(WebServerPath & VirtualPath & "/" & fi.Name)
            i += 1
        Next
        Return aFiles
    End Function

    <WebMethod(Description:="Retrieve a single file.")> _
    Public Function GetFile(ByVal VirtualPath As String, ByVal FileName As String) As Byte()
        Return ReadFile(WebServerPath & VirtualPath & "/" & FileName)
    End Function

    Private Shared Function ReadFile(ByVal FilePath As String) As Byte()
        Dim fs As FileStream
        Try
            ' Read file and return contents
            fs = File.Open(FilePath, FileMode.Open, FileAccess.Read)
            Dim lngLen As Long = fs.Length
            Dim abytBuffer(CInt(lngLen - 1)) As Byte
            fs.Read(abytBuffer, 0, CInt(lngLen))
            Return abytBuffer
        Catch exp As Exception
            Return Nothing
        Finally
            If Not fs Is Nothing Then
                fs.Close()
            End If
        End Try
    End Function

    <WebMethod(Description:="Upload a single file to web server.")> _
    Public Function UploadFile(ByVal VirtualPath As String, ByVal Name As String, ByVal Content As Byte()) As Boolean
        Dim objFile As File, objStream As StreamWriter, objFstream As FileStream
        Try
            objFstream = File.Open(WebServerPath & VirtualPath & "/" & Name, FileMode.Create, FileAccess.Write)
            Dim lngLen As Long = Content.Length
            objFstream.Write(Content, 0, CInt(lngLen))
            objFstream.Flush()
            objFstream.Close()
            Return True
        Catch exc As System.UnauthorizedAccessException
            Return False
        Catch exc As Exception
            Return False
        Finally
            If Not objFstream Is Nothing Then
                objFstream.Close()
            End If
        End Try
    End Function


#Region " Helper Function "
    <WebMethod(Description:="Retrieves version & copyright information about this web service.")> Public Function About() As String
        ' Uses the StringWriter to build a string with carriage returns & line feeds to
        ' return back to a calling client the Title, Version, and Description by
        ' reading Assembly meta-data originally entered in the AssemblyInfo.vb file
        ' using the AssemblyInfo class defined in the same file.

        Try
            Dim sw As New System.IO.StringWriter()
            Dim ainfo As New AssemblyInfo()

            With sw
                .WriteLine(ainfo.Title)
                .WriteLine(String.Format("Version {0}", ainfo.Version))
                .WriteLine(ainfo.Copyright)
                .WriteLine("")
                .WriteLine(ainfo.Description)
                .WriteLine("")
                .WriteLine(ainfo.CodeBase)
            End With

            Dim strRetVal As String = sw.ToString
            sw.Close()

            Return strRetVal
        Catch exp As Exception
            Return exp.Message
        End Try
    End Function
#End Region

End Class

阅读全文
0 0

相关文章推荐

img
取 消
img