我目前正在使用 .NET SDK 从 Amazon S3 下载文件,我目前有以下代码来执行此操作(仅允许使用这些文件):
With request
.WithBucketName(bucketName)
.WithKey(key)
End With
response2 = client.GetObject(request)
Dim strReader As MemoryStream = New MemoryStream
response2.ResponseStream.CopyTo(strReader)
Response.ContentType = getContentType(key)
Response.OutputStream.Write(strReader.GetBuffer, 0, strReader.GetBuffer.Length)
Dim fileName As String = Path.GetFileName(key)
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName)
Return ""
End Function
Private Function getContentType(ByVal fileToContent As String) As String
Dim fileExtension As String = Path.GetExtension(fileToContent)
Dim contentType As String
Select Case fileExtension
Case ".bmp"
contentType = "image/bmp"
Case ".png"
contentType = "image/png"
Case ".xlsx", ".xls"
contentType = "application/vnd.ms=excel"
Case ".jpg", ".jpeg", ".gif"
contentType = "image/jpeg"
Case ".pdf"
contentType = "application/pdf"
Case ".ppt", ".pptx"
contentType = "application/vnd.ms-powerpoint"
Case ".doc", ".docx"
contentType = "application/msword"
Case Else
contentType = "text/plain"
End Select
Return contentType
End Function
我有 2 个问题:首先,当我尝试在客户端窗口上打开文件时,告诉我(对于 MS Office 文件)文件已损坏,有时它会设法打开它们,有时却没有。其次,似乎如果我的文件具有.pptx 之类的扩展名,并且我在内容类型中说“PowerPoint”,那么浏览器似乎会尝试向它们附加一个扩展名,例如“.ppt”或“.doc”。有什么办法可以解决吗?
编辑:打开 MS Office 文件时收到的实际消息:'PowerPoint 在 PowerPointFile.ppt 中发现不可读的内容。是否要恢复此演示文稿的内容?如果您信任此演示文稿的来源,请单击是。