none
Open or Download document in HTML 5 using WCF Service RRS feed

  • Question

  • We are building a mobile app using HTML5 and we are having some issues opening or downloading a document. We are using wcf rest service to get document from server as file stream or byte array.

    Once we get data from WCF service(stream or byte array) we want to convert it back to document using javasript or jquery and download it.

    'Return Document as Byte() Array 
    Public Function GetFile(fileName As String) As Byte()
            Dim fs As FileStream = Nothing
            Try
    
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream"
                fs = New FileStream(fileName, FileMode.Open)
    
                Dim length As Integer = CInt(fs.Length)
                WebOperationContext.Current.OutgoingResponse.ContentLength = length
                Using memoryStream = New MemoryStream()
                    fs.CopyTo(memoryStream)
                    Return memoryStream.ToArray()
                End Using
            Catch ex As Exception
                strError = ex.Message & vbNewLine & ex.StackTrace
                Return Nothing
            Finally
                If fs IsNot Nothing Then fs.Close()
            End Try
        End Function
    'Return Document as FileStream 
    Public Function DownloadFile(fileName As String) As Stream
     Try
         Dim headerInfo As [String] = "attachment; filename=" & fileName
         WebOperationContext.Current.OutgoingResponse.Headers("Content-Disposition") = headerInfo
         WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream"
         ReturnFile.OpenRead(fileName)
    Catch ex AsException
         ReturnNothing
    EndTry
    End Function

    any idea how to implement this ?

    Thanks,

    Dishant

    Tuesday, October 7, 2014 6:31 AM

Answers