Get attachment stream in C#

Stephen P asked on September 6, 2017 15:26

Hello. I am trying to retrieve the binary stream for an attachment from its GUID ID so that I can modify it in a response. So far I have been able to get the attachment info:

    Guid attachmentGuid = document.GetGuidValue("FileAttachment", Guid.Empty);

    var attachment = AttachmentInfoProvider.GetAttachmentInfo(attachmentGuid, SiteContext.CurrentSiteName);

However, the AttachmentInfo instance returned does not provide me with a stream.

I've also tried:

var attachment = DocumentHelper.GetAttachment(document, attachmentGuid, true);

(and various overloads) but the AttachmentBinary property is null.

How do I get the stream for the attachment?

I'm on Kentico version 10.

Recent Answers


Suneel Jhangiani answered on September 6, 2017 16:18

You can get a byte[] using the AttachmentBinaryHelper.GetAttachmentBinary(DocumentAttachment) method.

If you really do need a stream then:

IO.FileInfo info = IO.FileInfo.New(AttachmentName + AttachmentExtension);
IO.FileStream stream = info.OpenRead();
0 votesVote for this answer Mark as a Correct answer

Stephen P answered on September 6, 2017 16:32

Thanks for the quick answer Suneel.

Unfortunately, AttachmentBinaryHelper.GetAttachmentBinary(...) returns null. The DocumentAttachment parameter I'm passing in is the one returned by DocumentHlper.GetAttachment(...) - presumably that is correct?

And the attachment isn't a file on disk, so the file IO won't work here.

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on September 8, 2017 02:18

I will presume that since you are saying the attachment isn't a file on disk that you are storing the attchment in the Database. In that case the AttachmentBinaryHelper.GetAttachmentBinary() method should return the data since it just pulls the AttachmentBinary field from the database. However, if you have Kentico set to store files in the filesystem that routine would be looking there and hence why you might be getting a null result.

Since it should be in the database you should be able to get to the Binary data like this:

Guid attachmentGuid = document.GetGuidValue("FileAttachment", Guid.Empty);
var attachment = AttachmentInfoProvider.GetAttachments()
    .TopN(1)
    .WhereEquals("AttachmentGUID", attachmentGuid)
    .BinaryData(true)
    .FirstOrDefault<AttachmentInfo>(); 
byte[] binaryData = attachment.AttachmentBinary;
0 votesVote for this answer Mark as a Correct answer

Stephen P answered on September 8, 2017 09:57

Thanks Suneel.

It appears that somehow my system had got into a state where the binary was neither on disk nor in the database. We had recently moved from file storage to database storage so something may have gone wrong there.

In any case, both DocumentHelper.GetAttachment and AttachmentBinaryHelper.GetAttachmentBinary work correctly, so thank you.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.