copy pdf file from local directory to sql database

Discussion in 'ASP.NET / ASP.NET Core' started by argeam, Jul 2, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hi, i'm doing a project on generating a crystalreport and viewing it in a pdf format. I'm using vb.net and sql server.

    i managed to general the crystalreport, save it in local directory then view it. but on the other hand i would also like to save the pdf file into my sql database. this is the difficult part and also the part which i'm stuck now. follows are all my code. hope there will be someone who can help me! thanks!

    Dim myConnection As New SqlClient.SqlConnection()
    myConnection.ConnectionString = "server=(local);database=st;Trusted_Connection=yes;timeout =45"
    Dim MyCommand As New SqlClient.SqlCommand()
    MyCommand.Connection = myConnection
    Dim CommandStr As String

    CommandStr = "execute usp_rpt_note @note_id= '" & Label_NoteID.Text & "'"
    MyCommand.CommandText = CommandStr
    MyCommand.CommandType = CommandType.Text
    Dim MyDA As New SqlClient.SqlDataAdapter()
    MyDA.SelectCommand = MyCommand
    Dim myDS As New Note_DataSet()

    MyDA.Fill(myDS, "usp_rpt_note")
    Dim oRpt As New Note_CrystalReport1()
    oRpt.SetDataSource(myDS)

    oRpt.ResourceName = "Note_CrystalReport1.rpt"

    Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
    oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
    oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

    Dim filename, filenamepdf, filenamexls As String
    filename = UCase(Logon_Name(Request.ServerVariables("LOGON_user"))) & CStr(Year(Now())) & CStr(Month(Now())) & CStr(Day(Now())) & CStr(Hour(Now())) & CStr(Minute(Now())) & CStr(Second(Now())) & CStr(CInt(Int((100000 * Rnd(200)) + 1)))
    filename = filename

    filenamepdf = "D:\Inetpub\wwwroot\report\temp\" & filename & ".pdf"

    DiskOpts.DiskFileName = filenamepdf

    oRpt.ExportOptions.DestinationOptions = DiskOpts
    oRpt.Export()

    ' Database operation variables
    Dim connect As New SqlClient.SqlConnection("Initial Catalog=st;Data Source=localhost;Integrated Security=SSPI;Persist Security Info=True;")
    Dim cmd As New SqlClient.SqlCommand("usp_copy_frm_directory", connect)
    Dim p As SqlClient.SqlParameter
    Dim uploaded_file As String
    Dim strFileUpload As String

    'uploaded_file = Request.QueryString("strFileName")
    strFileUpload = "d:\Inetpub\wwwroot\report\temp\" & filename & ".pdf"
    filename = strFileUpload

    'File/data variables
    Dim imageStream As New IO.FileStream(filename, IO.FileMode.Open)

    'Dim imagestream As New IO.FileStream(Server.MapPath("ToBeUpload.doc"), IO.FileMode.Open)
    Dim imageData(imageStream.Length) As Byte
    Dim FileSize As Integer = CType(imageStream.Length, Integer)
    Dim UploadDate As DateTime = DateTime.Now
    'Dim sContentType As String = imageStream.GetType.ToString 'File1.PostedFile.ContentType
    Dim sContentType As String = "Application/pdf"
    'Dim sContentType As System.IO.Stream = File1.PostedFile.InputStream
    ' Read the image data and close the stream
    imageStream.Read(imageData, 0, imageStream.Length)
    imageStream.Close()

    ' Save the image
    cmd.CommandType = CommandType.StoredProcedure

    p = New SqlClient.SqlParameter("@ImageData", SqlDbType.Image)
    p.Value = imageData
    cmd.Parameters.Add(p)

    p = New SqlClient.SqlParameter("@ContentType", SqlDbType.NVarChar)
    p.Value = sContentType
    cmd.Parameters.Add(p)

    connect.Open()

    cmd.ExecuteNonQuery()
    connect.Close()

    cmd.Dispose()
    connect.Dispose()

    Dim strPDF As String
    strPDF = "./temp/" & filename & ".pdf"
    Response.Redirect(strPDF)

    Somehow it didnt work! i'm calling the above codes as a function in my pageload.

    Hope help is given! thanks!!
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page