【问题标题】:Downloading Amazon S3 objects, Windows says files are corrupted下载 Amazon S3 对象,Windows 说文件已损坏
【发布时间】:2012-11-29 21:38:04
【问题描述】:

我目前正在使用 .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 中发现不可读的内容。是否要恢复此演示文稿的内容?如果您信任此演示文稿的来源,请单击“是”。

【问题讨论】:

  • 关于 #2,您需要为.***x Office 文档使用不同的 MIME 类型。见here
  • 好的,我现在打开文件没有问题,尽管在打开文件之前我仍然会从 Windows 收到神秘的错误消息。

标签: c# .net vb.net amazon-s3 amazon-web-services


【解决方案1】:

好的,对于#1,不要在 MemoryStream 上使用GetBuffer。使用ToArray:

Dim bytes = strReader.ToArray()
Response.OutputStream.Write(bytes, 0, bytes.Length) 

GetBuffer 返回缓冲区,而不是写入流的内容。

对于 #2,您需要为 .***x Office 文档使用不同的 MIME 类型。见here

【讨论】:

    【解决方案2】:

    以下代码将从该位置获取文件并自动将其下载到您的浏览器中,而不会收到文件损坏警告。

    using(var response = client.GetObjectAsync(request).Result) 
    {
      using( var responseStream = response.ResponseStream )
      {
    
        Response.ContentType = response.Headers.ContentType;
        Response.AddHeader("Content-Length", response.Headers.ContentLength.ToString());
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + uri.Key + "\"");
        Response.ContentType = "application/octet-stream";
        
        var memoryStream = new MemoryStream();
        responseStream.CopyTo(memoryStream);
        
        Response.BinaryWrite(memoryStream.ToArray()); //All byte data is converting to files                                     
        
      }
    }
    

    【讨论】:

    • 我很欣赏除了为接受的答案提供不同的方法之外,这还提供了更多的上下文。不错的第一篇文章。
    【解决方案3】:

    感谢您的努力,但我们可以通过以二进制格式发送正文中的文件来解决此问题。

    const uploadS3 = (url,file) =>{
      var requestOptions = {
        method: 'PUT',
        body: file,
        redirect: 'follow',
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2014-08-09
      • 1970-01-01
      相关资源
      最近更新 更多