asp.net - 网站不会发布使用 openxml 生成的文件
问题描述
情况如下:
Asp.Net Web Forms 站点使用 Open XML 读取(通过流)word 文档 (docx)。然后我在文档中插入一些文本,然后将文件写回不同的位置。然后通过电子邮件将其发送给最终用户。所有这一切都很好。
我遇到的问题是我不能由网站编写新文件。我收到以下错误:“该进程无法访问该文件(此处为文件名),因为它正在被另一个进程使用”
我已确认保留该文件的是站点(或 IIS)。
下面是读取原始文件并生成新文件的代码:
Private Function GetDocument(worddoc As String) As Integer
Dim byteArray As Byte() = File.ReadAllBytes("\\WEB-DEV-1\HR_Documents\" & worddoc)
Using Stream As New MemoryStream()
Stream.Write(byteArray, 0, CInt(byteArray.Length))
Try
'Set Row & Cell variables
Dim rowNum As Integer = 0
Dim cellNum As Integer = 0
'Set File Stream
Using doc As WordprocessingDocument = WordprocessingDocument.Open(Stream, True)
'Employee Name Insert
'Find first table in document
Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
'First Row in tbl
Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
'Find first cell in row
Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
'Insert selected Employee Name
Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
Dim r As Run = p.Elements(Of Run)().First()
Dim txt As Text = r.Elements(Of Text)().First()
txt.Text = "Employee Name: " & ddlEmployeeList.SelectedItem.Text
'Supervisor Name Insert
'Check for form
If ddlFormChoice.SelectedIndex <> 2 Then
'Reset row to supervisors row in table
row = tbl1.Elements(Of TableRow)().ElementAt(1)
ElseIf ddlFormChoice.SelectedIndex = 2 Then
'Reset row to supervisors row in table
row = tbl1.Elements(Of TableRow)().ElementAt(2)
End If
If ddlFormChoice.SelectedIndex <> 2 Then
'Reset cell to supervisor cell in row
cell = row.Elements(Of TableCell)().ElementAt(1)
ElseIf ddlFormChoice.SelectedIndex = 2 Then
'Reset cell to supervisor cell in row
cell = row.Elements(Of TableCell)().ElementAt(0)
End If
'Insert selected Employee Name
p = cell.Elements(Of Paragraph)().First()
r = p.Elements(Of Run)().First()
txt = r.Elements(Of Text)().First()
If ddlFormChoice.SelectedIndex <> 2 Then
txt.Text = "Supervisor: " & ddlSupervisorList.SelectedItem.Text
ElseIf ddlFormChoice.SelectedIndex = 2 Then
txt.Text = "Manager/Supervisor: " & ddlSupervisorList.SelectedItem.Text
End If
doc.Close()
End Using
'Save File to temp location
File.WriteAllBytes("\\WEB-DEV-1\HR_Documents\TempDocs\" & worddoc, Stream.ToArray())
Stream.Close()
Stream.Dispose()
Return 1
Catch ex As Exception
Return Nothing
End Try
End Using
End Function
我关闭了 OpenXML 文档和流以及处理流,但是当我尝试从调用该函数的主子中删除文件时,我得到了上面列出的错误。
我错过了什么??我关闭了文档、流并处理了流。为什么该站点仍保留该文件?
注意这里试图删除文件的代码行;
File.Delete("\\Web-Dev-1\HR_Documents\TempDocs\" & fileAttach)
解决方案
因此,经过一天的大部分时间,我终于发现了问题所在。在创建、保存并通过电子邮件发送文档后,它由电子邮件方法保存。出于某种原因,我认为当方法完成时它会处理邮件消息,但事实并非如此。
一旦我添加了 dispose 行,它就一切正常了。
只在谷歌上搜索了将近两天。:|
推荐阅读
- python - LRANGE 的 Redis 性能问题在一个大列表中
- python-3.x - 使用 python dpath 库删除嵌套对象
- typescript - 有没有办法从重载的 TS 方法中提取所有函数签名?
- json - 我们如何将 JSON 对象转换为 JAVA 中的结果集
- javascript - 如何使用 C# 执行 HTML 文件的所有 Javascript 以仅生成 HTML DOM
- ggplot2 - 我怎样才能让线条出现在点之外?
- linux - 显示构建命令
- ios - 如何使用 RealityKit 将 3D 模型放置在参考图像之上?
- reactjs - 单击按钮时更改网格
- asp.net - Search() 操作方法返回 Index View() 时出错