控制器:
<HttpGet>
Function downloadpdf() As FileContentResult
Dim data As MyData = Me.Getdata()
Dim sb As StringBuilder = New StringBuilder()
If data IsNot Nothing Then
If data.HasItems Then
sb.AppendLine()
sb.AppendLine("ID,Description)
For Each item In data.Items
sb.AppendLine(String.Format("{0},{1}", item.Id, item.Description))
Next
End If
End If
Dim bytes as Byte() = New UTF8Encoding().GetBytes(sb.ToString())
Response.Clear()
Response.AddHeader("Content-Disposition", "inline; filename=mypdf.pdf")
Response.AddHeader("Content-Type", "application/pdf")
Response.ClearHeaders()
Response.AddHeader("Content-Length", bytes.Length.ToString())
Return File(bytes, "application/pdf", "mypdf.pdf")
End Function
查看:
<a href="@Url.Action("downloadpdf", "home")">PDF</a>
点击链接后,会提示下载文件,然后显示:
Adobe Acrobat Reader无法打开mypdf.pdf&#39;因为它不是受支持的文件类型,或者因为文件已损坏(例如,它是作为电子邮件附件发送的,并且未正确解码)。
pdf没有打开。 有任何建议或任何其他方式吗?
干杯