下载后文件损坏
问题描述:
我在ASP.NET(VB.NET)中下载后打开文件时收到以下错误 - 远程服务器共享路径中有文件。下载后文件损坏
使用下面的代码是下载一个文件:
Private Sub downloadFile(ByVal file As String)
Try
Dim requestFile As String = "\\ashleyfurniture\afi-dfs\Arcadia\Vaults\Web\AshleyDirectAttachments\IdeaNetwork\Images" + "\" + file
If String.IsNullOrEmpty(requestFile) Then
Throw New FileNotFoundException("File to download cannot be null or empty")
End If
' Get file name from URI string in C#
Dim uri = New Uri(requestFile)
Dim filename As String = Path.GetFullPath(uri.OriginalString)
Dim fileInfo = New FileInfo((uri.OriginalString))
If Not fileInfo.Exists Then
Throw New FileNotFoundException("File to download was not found", uri.OriginalString)
End If
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=""" + fileInfo.Name + """")
Response.WriteFile(fileInfo.FullName)
Response.End()
' ignore exception
Catch generatedExceptionName As ThreadAbortException
Catch ex As FileNotFoundException
Response.StatusCode = CInt(System.Net.HttpStatusCode.NotFound)
Response.StatusDescription = ex.Message
Catch ex As Exception
Response.StatusCode = CInt(System.Net.HttpStatusCode.InternalServerError)
Response.StatusDescription = String.Format("Error downloading file: {0}", ex.Message)
End Try
End Sub
答
与Wiktor的Zychla同意
尝试删除您回应
Response.ContentType = "application/octet-stream"
的contentType中如果您在打开损坏的文件十六进制编辑器(甚至是文本编辑器),它是否以一些看起来像网页的文本开始? –
明确地缺少'Response.Clear()'在'Response.ContentType'之前,以确保管道不会在这里附加当前处理的页面。我还会在Response.End()之前添加'Response.Flush()'。 –