GUARDAR Y ABRIR DOCUMENTOS PDF EN ASP .NET
Publicado por Manuel Lambis Ortiz (1 intervención) el 12/01/2009 23:08:03
Hola publico, les envio saludos a quien le interese mi nota, estuve realizando una practica donde mi proposito guardar archivos pdf en sql server y posteriormente descargarlo en una pagina .aspx, lo logre implementandolo en el lenguaje visual basic, no es complicado solo hay que tener tadas las herramientas del lenguaje clara, aqui les dejo el codigo para quien le interese y lo utilize en sus trabajos...
CLASE EN VISUAL BASIC PARA CARGAR EL ARCHIVO AL SERVIDOR Y GUARDARLO EN LA BASE DA DATOS
Imports System.Data.Sql
Imports System.IO
Imports System.Data.SqlClient
Partial Public Class IngresaPDF
Inherits System.Web.UI.Page
Dim tam As Integer
Dim nom As String
Dim sql As String
Dim ruta As String
Dim arreglo() As Char
'lectura de archivo
Dim archivo As FileStream
'conexion a BD
Dim conexion As SqlConnection
Dim sentencia As SqlCommand
Dim cadena As String = "Data Source = SERVIDOR BASE DE DATOS;
Initial Catalog =BASE DE DATOS; Integrated Security = True"
'parametros para procedimiento almacenado
Dim autor As SqlParameter
Dim apell As SqlParameter
Dim nombre As SqlParameter
Dim tama As SqlParameter
Dim arch As SqlParameter
Dim sw As Integer
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If (FileUpload1.HasFile) Then
Try
'Obtener parametros a utilizar
nom = FileUpload1.FileName
tam = FileUpload1.FileBytes.Length
'NOTA: EL ARCHIVO PDF PRIMERO LO GUARDO EN UNA CARPETA EN EL
'SERVIDOR Y LUEGO LO LEO LOCALMENTE PARA GUARDARLO EN LA BD (FOLDER)
ruta = "C:folder" & nom
'Guardar Archivo en Carpeta Servidor
FileUpload1.SaveAs(ruta)
'leer archivo
archivo = New FileStream(ruta, FileMode.Open, FileAccess.Read)
Dim imagen(tam) As Byte
archivo.Read(imagen, 0, tam)
archivo.Close()
'Conexion a Base de Datos
conexion = New SqlConnection()
conexion.ConnectionString = cadena
'procedimiento almacenado
sql = "doc"
sentencia = New SqlCommand(sql, conexion)
sentencia.CommandType = CommandType.StoredProcedure
conexion.Open()
'parametros procedimiento almacenado
autor = New SqlParameter("@autor", SqlDbType.VarChar)
apell = New SqlParameter("@apell", SqlDbType.VarChar)
nombre = New SqlParameter("@pdf", SqlDbType.VarChar)
tama = New SqlParameter("@tam", SqlDbType.VarChar)
arch = New SqlParameter("@archivo", SqlDbType.Image)
'Establecer valores direccion del parametro
autor.Value = TextBox1.Text
autor.Direction = ParameterDirection.Input
apell.Value = TextBox2.Text
autor.Direction = ParameterDirection.Input
nombre.Value = nom
nombre.Direction = ParameterDirection.Input
tama.Value = tam
tama.Direction = ParameterDirection.Input
arch.Value = imagen
arch.Direction = ParameterDirection.Input
'agregar parametros al procedimiento
sentencia.Parameters.Add(autor)
sentencia.Parameters.Add(apell)
sentencia.Parameters.Add(nombre)
sentencia.Parameters.Add(tama)
sentencia.Parameters.Add(arch)
sw = sentencia.ExecuteNonQuery()
If (sw <> 0) Then
Label1.Text = "Archivo Guardado Satisfactoriamente"
End If
Catch ex As Exception
Label1.Text = "Problemas al Guardar Archivo: " & ex.Message
End Try
End If
End Sub
End Class
CREAR EL SIGUIENTE PROCEDIMIENTO ALMACENADO EN SQL SERVER PARA GUARDAR EL ARCHIVO PDF YA QUE NO ES POSIBLE CON LA SENTENCIA INSERT DIRECTAMENTE (HAY QUE CREAR LA TABLA DOCUMENTOS)
create procedure doc
@autor varchar(50)=null,
@apell varchar(50)=null,
@pdf varchar(50)=null,
@tam varchar(50)=null,
@archivo image=null
as
insert into documentos values(@autor,@apell,@pdf,@tam,@archivo)
POR ULTIMO LA CLASE PARA VISUALIZAR EL PDF EN EL NAVEGADOR WEB
LA DESCARLA DEL DOCUMENTO LA REALIZO POR LA INSERCION DEL NOMBRE Y APELLIDO DE QUIEN SUBIO EL PDF A LA BD, EN ESTE CASO PUEDES HACERLO CON UN SELECT O CON UN PROCEDIMIENTO ALMACENADO
CLASE DE DESCARGA DEL DOCUMENTO PDF
Imports System.Data.SqlClient
Imports System.IO
Partial Public Class DescargarPDF
Inherits System.Web.UI.Page
'conexion a BD
Dim conexion As SqlConnection
Dim sentencia As SqlCommand
Dim imagen As SqlDataReader
Dim cadena As String = "Data Source = SERVIDOR DB; Initial Catalog =BD;
Integrated Security = True"
Dim sql As String
'parametros entrada procedimiento almacenada
Dim aut As SqlParameter
Dim ape As SqlParameter
Dim documento As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'conectara base de datos
conexion = New SqlConnection()
conexion.ConnectionString = cadena
sql = "select img from documentos where aut='" & TextBox1.Text & "'and apel='" & TextBox2.Text & "'"
sentencia = New SqlCommand(sql, conexion)
conexion.Open()
aut = New SqlParameter("@nombre", SqlDbType.VarChar)
ape = New SqlParameter("@apellido", SqlDbType.VarChar)
'parametros procedimiento almacenado
aut.Value = TextBox1.Text
aut.Direction = ParameterDirection.Input
ape.Value = TextBox2.Text
ape.Direction = ParameterDirection.Input
'agregar parametros
sentencia.Parameters.Add(aut)
sentencia.Parameters.Add(ape)
'ejecutar comando sql
imagen = sentencia.ExecuteReader()
Dim conv As New System.Text.ASCIIEncoding
'mostrar documentos pdf
If (imagen.Read) Then
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.BinaryWrite(imagen(0))
End If
End Sub
End Class
AUTOR: MANUEL LAMBIS ORTIZ
SALUDOS
CLASE EN VISUAL BASIC PARA CARGAR EL ARCHIVO AL SERVIDOR Y GUARDARLO EN LA BASE DA DATOS
Imports System.Data.Sql
Imports System.IO
Imports System.Data.SqlClient
Partial Public Class IngresaPDF
Inherits System.Web.UI.Page
Dim tam As Integer
Dim nom As String
Dim sql As String
Dim ruta As String
Dim arreglo() As Char
'lectura de archivo
Dim archivo As FileStream
'conexion a BD
Dim conexion As SqlConnection
Dim sentencia As SqlCommand
Dim cadena As String = "Data Source = SERVIDOR BASE DE DATOS;
Initial Catalog =BASE DE DATOS; Integrated Security = True"
'parametros para procedimiento almacenado
Dim autor As SqlParameter
Dim apell As SqlParameter
Dim nombre As SqlParameter
Dim tama As SqlParameter
Dim arch As SqlParameter
Dim sw As Integer
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If (FileUpload1.HasFile) Then
Try
'Obtener parametros a utilizar
nom = FileUpload1.FileName
tam = FileUpload1.FileBytes.Length
'NOTA: EL ARCHIVO PDF PRIMERO LO GUARDO EN UNA CARPETA EN EL
'SERVIDOR Y LUEGO LO LEO LOCALMENTE PARA GUARDARLO EN LA BD (FOLDER)
ruta = "C:folder" & nom
'Guardar Archivo en Carpeta Servidor
FileUpload1.SaveAs(ruta)
'leer archivo
archivo = New FileStream(ruta, FileMode.Open, FileAccess.Read)
Dim imagen(tam) As Byte
archivo.Read(imagen, 0, tam)
archivo.Close()
'Conexion a Base de Datos
conexion = New SqlConnection()
conexion.ConnectionString = cadena
'procedimiento almacenado
sql = "doc"
sentencia = New SqlCommand(sql, conexion)
sentencia.CommandType = CommandType.StoredProcedure
conexion.Open()
'parametros procedimiento almacenado
autor = New SqlParameter("@autor", SqlDbType.VarChar)
apell = New SqlParameter("@apell", SqlDbType.VarChar)
nombre = New SqlParameter("@pdf", SqlDbType.VarChar)
tama = New SqlParameter("@tam", SqlDbType.VarChar)
arch = New SqlParameter("@archivo", SqlDbType.Image)
'Establecer valores direccion del parametro
autor.Value = TextBox1.Text
autor.Direction = ParameterDirection.Input
apell.Value = TextBox2.Text
autor.Direction = ParameterDirection.Input
nombre.Value = nom
nombre.Direction = ParameterDirection.Input
tama.Value = tam
tama.Direction = ParameterDirection.Input
arch.Value = imagen
arch.Direction = ParameterDirection.Input
'agregar parametros al procedimiento
sentencia.Parameters.Add(autor)
sentencia.Parameters.Add(apell)
sentencia.Parameters.Add(nombre)
sentencia.Parameters.Add(tama)
sentencia.Parameters.Add(arch)
sw = sentencia.ExecuteNonQuery()
If (sw <> 0) Then
Label1.Text = "Archivo Guardado Satisfactoriamente"
End If
Catch ex As Exception
Label1.Text = "Problemas al Guardar Archivo: " & ex.Message
End Try
End If
End Sub
End Class
CREAR EL SIGUIENTE PROCEDIMIENTO ALMACENADO EN SQL SERVER PARA GUARDAR EL ARCHIVO PDF YA QUE NO ES POSIBLE CON LA SENTENCIA INSERT DIRECTAMENTE (HAY QUE CREAR LA TABLA DOCUMENTOS)
create procedure doc
@autor varchar(50)=null,
@apell varchar(50)=null,
@pdf varchar(50)=null,
@tam varchar(50)=null,
@archivo image=null
as
insert into documentos values(@autor,@apell,@pdf,@tam,@archivo)
POR ULTIMO LA CLASE PARA VISUALIZAR EL PDF EN EL NAVEGADOR WEB
LA DESCARLA DEL DOCUMENTO LA REALIZO POR LA INSERCION DEL NOMBRE Y APELLIDO DE QUIEN SUBIO EL PDF A LA BD, EN ESTE CASO PUEDES HACERLO CON UN SELECT O CON UN PROCEDIMIENTO ALMACENADO
CLASE DE DESCARGA DEL DOCUMENTO PDF
Imports System.Data.SqlClient
Imports System.IO
Partial Public Class DescargarPDF
Inherits System.Web.UI.Page
'conexion a BD
Dim conexion As SqlConnection
Dim sentencia As SqlCommand
Dim imagen As SqlDataReader
Dim cadena As String = "Data Source = SERVIDOR DB; Initial Catalog =BD;
Integrated Security = True"
Dim sql As String
'parametros entrada procedimiento almacenada
Dim aut As SqlParameter
Dim ape As SqlParameter
Dim documento As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'conectara base de datos
conexion = New SqlConnection()
conexion.ConnectionString = cadena
sql = "select img from documentos where aut='" & TextBox1.Text & "'and apel='" & TextBox2.Text & "'"
sentencia = New SqlCommand(sql, conexion)
conexion.Open()
aut = New SqlParameter("@nombre", SqlDbType.VarChar)
ape = New SqlParameter("@apellido", SqlDbType.VarChar)
'parametros procedimiento almacenado
aut.Value = TextBox1.Text
aut.Direction = ParameterDirection.Input
ape.Value = TextBox2.Text
ape.Direction = ParameterDirection.Input
'agregar parametros
sentencia.Parameters.Add(aut)
sentencia.Parameters.Add(ape)
'ejecutar comando sql
imagen = sentencia.ExecuteReader()
Dim conv As New System.Text.ASCIIEncoding
'mostrar documentos pdf
If (imagen.Read) Then
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.BinaryWrite(imagen(0))
End If
End Sub
End Class
AUTOR: MANUEL LAMBIS ORTIZ
SALUDOS
Valora esta pregunta


0