3

I am currently downloading Files from Amazon S3 using the .NET SDK, I currently have the following code to do it (only these files are allowed):

    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

I am having 2 problems: first, when I try to open the files on the client windows tells me (for MS office files) that the files are corrupted, somtimes it manages to open them anyway, sometimes not. Second, it seems that if I the files have extensions like .pptx, and I say 'PowerPoint' in the content type, it seems like the browser tries to append an extension to them like '.ppt' or '.doc'. Any way to fix that?

EDIT: actual message that I am getting when opening a MS office file: 'PowerPoint found unreadable content in PowerPointFile.ppt. Do you want to recover the contents of this presentation? If you trust the source of this presentation, click Yes.

Claudio
  • 9,427
  • 3
  • 28
  • 67
Art F
  • 3,572
  • 10
  • 45
  • 74
  • Regarding #2, you need to use a different MIME type for `.***x` Office documents. See [here](http://stackoverflow.com/a/4212908/492405) – vcsjones Nov 29 '12 at 21:41
  • Ok, I have no issues opening the files now although I still get that mysterious error message from windows before opening them. – Art F Nov 29 '12 at 21:48

3 Answers3

3

OK, for #1, don't use GetBuffer on MemoryStream. Use ToArray:

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

GetBuffer returns the buffer, not what was written to the stream.

For #2, you need to use a different MIME type for .***x Office documents. See here.

Community
  • 1
  • 1
vcsjones
  • 128,004
  • 28
  • 283
  • 274
1

The following code will get the file from the location and download it into your browser automatically without you receiving a corrupted file warning.

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                                     
    
  }
}
Jeremy Caney
  • 4,585
  • 13
  • 32
  • 54
Debashis D
  • 21
  • 4
  • 1
    I appreciate that in addition to providing a different approach to the accepted answer, this also provides more context. Good first post. – Jeremy Caney Aug 27 '20 at 21:29
-1

I appreciate your efforts, but we can fix this issue by sending file in body in binary format.

const uploadS3 = (url,file) =>{
  var requestOptions = {
    method: 'PUT',
    body: file,
    redirect: 'follow',
};
Gabriel Wamunyu
  • 543
  • 7
  • 18