0
我在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
的contentType中如果您在打開損壞的文件十六進制編輯器(甚至是文本編輯器),它是否以一些看起來像網頁的文本開始? –
明確地缺少'Response.Clear()'在'Response.ContentType'之前,以確保管道不會在這裏附加當前處理的頁面。我還會在Response.End()之前添加'Response.Flush()'。 –