Modifica file word memorizzato in database sql express

venerdì 11 maggio 2012 - 20.12

Geronimo79 Profilo | Junior Member

Salve amici, il mio quesito è il seguente: uso asp.net 4 e vb.net inserisco in un database sql un file word, lo apro e nel momento in cui apporto delle modifiche ad esso vorrei che mi rimanessero salvate! Invece se salvo il documento e lo riapro mi riparte dal suo stato originale. Non ho idea di come procedere....è possibile ovviare al problema? Grazie a tutti.

Gluck74 Profilo | Guru

ciao,
c'è un problemino di fondo per quello che devi fare:
Quando apri un file da un browser, vuol dire che hai cliccato su un link o qualcosa del genere per richiedere il file al server.
Il file richiesto, viene inviato al browser (client), che effettua il download e lo salva localmente (temporary internet file).
Quindi le modifiche che tu effettui, le fai localmente. Quando chiudi, hai perso le modifiche.

Quello che dovresti fare è questo:
1 - Permettere il download del file
2 - Modificare il file localmente
3 - Effettuare un upload sul server per inviare le modifiche.

ciao
____________
http://glucolo.wordpress.com
Ricordati di utilizzare il tasto "Accetta" se i nostri consigli ti sono serviti a risolvere il problema.
È il modo per ringraziare chi ti ha aiutato.

Geronimo79 Profilo | Junior Member

Ciao e grazie per la risposta....avevo già implementato il tutto...questo è il codice:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim validFileTypes As String() = {"doc", "docx"}
Dim ext2 As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim isValidFile As Boolean = False
For i As Integer = 0 To validFileTypes.Length - 1
If ext2 = "." & validFileTypes(i) Then
isValidFile = True
Exit For
End If
Next
If Not isValidFile Then
Dim message2 As String = "Inserire file con estensione .doc oppure .docx !"
Dim sb2 As New System.Text.StringBuilder()
sb2.Append("<script type = 'text/javascript'>")
sb2.Append("window.onload=function(){")
sb2.Append("alert('")
sb2.Append(message2)
sb2.Append("')};")
sb2.Append("</script>")
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb2.ToString())
Return
Else
Dim message3 As String = "File inserito correttamente"
Dim sb3 As New System.Text.StringBuilder()
sb3.Append("<script type = 'text/javascript'>")
sb3.Append("window.onload=function(){")
sb3.Append("alert('")
sb3.Append(message3)
sb3.Append("')};")
sb3.Append("</script>")
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb3.ToString())
End If

Dim filePath As String = FileUpload1.PostedFile.FileName
Dim filename As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(filename)
Dim contenttype As String = String.Empty

Select Case ext
Case ".doc"
contenttype = "File Word"
Exit Select

Case ".docx"
contenttype = "File Word"
Exit Select

End Select

Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
br.Close()
fs.Close()
Dim strQuery As String = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "File Word"
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
InsertUpdateData(cmd)
End Sub

Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()
Dim con As New SqlConnection(strConnString)
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Response.Write(ex.Message)
Return False
Finally
con.Close()
con.Dispose()
End Try
End Function


Public Function GetData(ByVal cmd As SqlCommand) As DataTable
Dim dt As New DataTable
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()
Dim con As New SqlConnection(strConnString)
Dim sda As New SqlDataAdapter
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
sda.SelectCommand = cmd
sda.Fill(dt)
Return dt
Catch ex As Exception
Response.Write(ex.Message)
Return Nothing
Finally
con.Close()
sda.Dispose()
con.Dispose()
End Try
End Function


Protected Sub download(ByVal dt As DataTable)
Dim bytes() As Byte = CType(dt.Rows(0)("Data"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = dt.Rows(0)("ContentType").ToString()
Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("Name").ToString())
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End Sub

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim strQuery As String = "select Name, ContentType, Data from tblFiles where id=@id"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("@id", SqlDbType.Int).Value = GridView1.SelectedValue
Dim dt As DataTable = GetData(cmd)
If dt IsNot Nothing Then
download(dt)
End If
End Sub

Quello che dici tu è esattamente quello che non voglio fare....
Io client apro il file nel database situato nel server lo modifico e lo vorrei salvare direttamente senza farne una copia e reinserirlo sul server...
Probabilmente non è possibile però...
Un saluto. :-)

Gluck74 Profilo | Guru

>Quello che dici tu è esattamente quello che non voglio fare....
>Io client apro il file nel database situato nel server lo modifico
>e lo vorrei salvare direttamente senza farne una copia e reinserirlo
>sul server...
>Probabilmente non è possibile però...
>Un saluto. :-)

Esatto, non è possibile con una semplice pagina web.
ciao buon lavoro
____________
http://glucolo.wordpress.com
Ricordati di utilizzare il tasto "Accetta" se i nostri consigli ti sono serviti a risolvere il problema.
È il modo per ringraziare chi ti ha aiutato.
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5