我有一个VB.net程序。用户下载,打开,保存并将word文档上载到从中下载的服务器。我用来上传文件的程序需要传递参数。
该程序适用于我尝试上传的大约80%的文档,但是对于那些不起作用的文档,在用户保存文件和上传到服务器之间会发生某些事情。
当我打开出现此问题的文档时,打开时会收到内存不足错误。
这是我正在使用的程序的代码:
Imports Microsoft.Office.Interop
Imports System
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Threading
Public Class Form1
' Private WithEvents oWord As Word.Application
Private trd As Thread
Dim WithEvents oWord As New Microsoft.Office.Interop.Word.Application
Dim WithEvents wc As New System.Net.WebClient()
Dim list As New List(Of String)
Dim GetString As String
Dim FileWOEX As String
Dim ORGDoc As String
Dim MergeVar As String
Dim Server As String
Dim DocPath As String
Dim FileName As String
Dim ToPath As String
Dim INCOMP As String
Dim INNUM As String
Dim strCommonAppData As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim list As New List(Of String)
Dim linef As String
Dim x As Integer
Try
Const ssfCOMMONAPPDATA = &H23
strCommonAppData = CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path & "\MCCIPRS\"
Catch ex As Exception
MsgBox("Data File Path Not Found, an Error occurred during install, please uninstall and reinstall our program!")
oWord.Quit()
Me.Close()
End Try
Try
GetString = Command()
MergeVar = GetString.Substring(11, 1)
INCOMP = GetString.Substring(0, 2)
If (INCOMP.Substring(0, 1) = "0") Then
INCOMP = INCOMP.Substring(1, 1)
End If
INNUM = GetString.Substring(2, 9)
ORGDoc = GetString.Substring(12)
Server = ORGDoc.Substring(0, (ORGDoc.IndexOf("/", 9)) + 1)
DocPath = ORGDoc.Substring((ORGDoc.IndexOf("/", 9)) + 1)
FileWOEX = ORGDoc.Substring(0, (ORGDoc.Length) - 4)
FileName = IO.Path.GetFileName(DocPath)
ToPath = DocPath.Substring(0, (DocPath.Length - FileName.Length))
Catch ex As Exception
MsgBox("Data from Web Is Invalid:" & ex.ToString())
oWord.Quit()
Me.Close()
End Try
Try
If (System.IO.File.Exists(strCommonAppData & IO.Path.GetFileName(ORGDoc))) Then
System.IO.File.Delete(strCommonAppData & IO.Path.GetFileName(ORGDoc))
End If
My.Computer.Network.DownloadFile(ORGDoc, strCommonAppData & IO.Path.GetFileName(ORGDoc), "", "")
Catch ex As Exception
MsgBox("There was a Problem Retrieving the Form from the web Please ensure the file exists or Contact you Administrator" & ex.ToString())
oWord.Quit()
Me.Close()
End Try
Try
' Make Word visible.
oWord.Visible = True
oWord.Documents.Open(strCommonAppData & IO.Path.GetFileName(ORGDoc))
Catch ex As Exception
MsgBox("There was an error when opening Word please ensure Word was installed correctly" & ex.ToString())
oWord.Quit()
Me.Close()
End Try
Try
If MergeVar = "M" Then
trd = New Thread(Sub()
Using frm As New Form2
Application.Run(frm)
End Using
End Sub)
trd.Start()
Try
If (System.IO.File.Exists(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")) Then
System.IO.File.Delete(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")
End If
My.Computer.Network.DownloadFile(FileWOEX & ".txt", strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt", "", "")
Catch ex As Exception
MsgBox("There was an issue downloading the Macro File from Server " & ex.ToString())
oWord.Quit()
Me.Close()
End Try
Using r As StreamReader = New StreamReader(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")
' Store contents in this String.
' Read first line.
linef = Trim(r.ReadLine())
x = 0
Do While (Not linef Is Nothing)
' Add this line to list.
list.Add(linef.Substring(13, linef.IndexOf("(") - 13))
list.Add(linef.Substring(linef.IndexOf("(") + 2, (linef.IndexOf(")") - 3) - linef.IndexOf("(")))
Try
Call oWord.Run(list(x), list(x + 1))
Catch ex As Exception
trd.Abort()
MsgBox("There was an issue filling Macros within this Word file the display may be incorrect")
'MsgBox("Macro Issues: " & list(x) & " " & list(x + 1) & " " & ex.ToString())
End Try
' Read in the next line.
linef = r.ReadLine
x = x + 2
Loop
End Using
trd.Abort()
Else
x = 0
End If
Catch ex As Exception
trd.Abort()
MsgBox("There was an issue filling Macros within this Word file the display may be incorrect")
End Try
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
Me.Opacity = 0
End Sub
'create wordApplication object with the key word “WithEvents”
Private Sub oWord_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles oWord.DocumentBeforeClose
'oWord.ActiveDocument.Close(Word.WdSaveOptions.wdSaveChanges)
oWord.ActiveDocument.Close()
oWord.Quit()
Try
Dim filepath As String
Dim url As String = Server & "pdtest/PD026U.pgm"
'Dim url As String = "http://192.168.95.1:83/file.php"
filepath = strCommonAppData & IO.Path.GetFileName(ORGDoc)
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
request.PreAuthenticate = True
request.AllowWriteStreamBuffering = False
Dim boundary As String = System.Guid.NewGuid().ToString()
request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary)
request.Method = "POST"
request.Credentials = New System.Net.NetworkCredential("ehavermale", "ernie1")
' Build Contents for Post
Dim header As String = String.Format("--{0}", boundary)
Dim footer As String = header & "--"
Dim contents As New System.Text.StringBuilder()
Dim FileHead As New System.Text.StringBuilder()
' file
FileHead.AppendLine(header)
FileHead.AppendLine(String.Format("Content-Disposition: form-data; name=""upfile""; filename=""{0}""", IO.Path.GetFileName(filepath)))
FileHead.AppendLine("Content-Type: application/octet-stream")
FileHead.AppendLine()
contents.AppendLine(header)
contents.AppendLine("Content-Disposition: form-data; name=""task""")
contents.AppendLine()
contents.AppendLine("upload")
contents.AppendLine(header)
contents.AppendLine("Content-Disposition: form-data; name=""INCOMP""")
contents.AppendLine()
contents.AppendLine(INCOMP)
contents.AppendLine(header)
contents.AppendLine("Content-Disposition: form-data; name=""INCNUM""")
contents.AppendLine()
contents.AppendLine(INNUM)
contents.AppendLine(header)
contents.AppendLine("Content-Disposition: form-data; name=""ToPath""")
contents.AppendLine()
contents.AppendLine(ToPath)
' Footer
contents.AppendLine(footer)
' This is sent to the Post
Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(contents.ToString())
Dim FileBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(FileHead.ToString())
Dim SendFBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
request.ContentLength = bytes.Length + FileHead.Length + SendFBytes.Length
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(FileBytes, 0, FileBytes.Length)
requestStream.Write(SendFBytes, 0, SendFBytes.Length)
requestStream.Write(bytes, 0, bytes.Length)
requestStream.Flush()
requestStream.Close()
Using response As WebResponse = request.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
Dim strResponseData As String = reader.ReadToEnd()
End Using
End Using
End Using
Catch ex As Exception
MsgBox("There was an Error on File Upload Please Contact you Administrator for assistance : " & ex.ToString())
End Try
oWord.Application.Quit()
oWord = Nothing
Me.Close()
End Sub
End Class
当我编码并将文件写入流时,GetRequestStream中是否存在我缺少的内容?这段代码似乎是我的问题我只是不确定是什么导致它打破一些文件但不是全部。
答案 0 :(得分:0)
发现我的问题。事实证明我发送服务器的标头不是它需要的标头。所以我更改了它们并添加了一种编码类型,它起作用了!