Menu

Example of using attachments to the browser?

PDVD
2008-05-16
2013-04-22
  • PDVD

    PDVD - 2008-05-16

    Hi,

    Using SharpMimeTools, and it works fine!

    The only problem i'm faceing is 'streaming' attachments to the browser...

    I have this ASP.NET (VB.NET) code:

       Dim Buffer(attachment.Stream.Length) As Byte
       attachment.Stream.Read(Buffer, 0, CInt(attachment.Stream.Length))
       attachment.Stream.Close()
       Response.ContentType = "image/jpeg" 'I know this to be a jpg file...
       Response.BinaryWrite(Buffer)

    But this does not give me a valid image file... (if I try with a plain text attachment file, it won't work either)

    Anybody have a example somewhere?

     
  • Matt Drechsler

    Matt Drechsler - 2010-05-14

    Sure I'm replying to a 2 year old message, and to an empty forum, but what have I got to lose?

    I echo the sentiment of not being able to stream attachments, but in my case to a database, not directly to a web page.  It simply populates my BLOB with zeroes, padded to the correct size, UNLESS I use attachment.save first.   Is the .stream availability tied to .save ing first?  Is there a way to make it available without saving?

    Here is a snippet of my "working example":

                If message.Attachments IsNot Nothing Then
                    For Each attachment As anmar.SharpMimeTools.SharpAttachment In message.Attachments
                        attachment.Save("C:\temp\", True)
                        Dim Buffer(attachment.Stream.Length) As Byte
                        attachment.Stream.Read(Buffer, 0, CInt(attachment.Stream.Length))
                        attachment.Stream.Close()
                        InsertAttachment(id, [Enum].GetName(GetType(MimeTopLevelMediaType), attachment.MimeTopLevelMediaType) & "/" & attachment.MimeMediaSubType, attachment.Name,  Buffer))
                    Next
                End If
    
     
  • Angel Marin

    Angel Marin - 2010-05-14

    What's the exact SharpMessage constructor you're using?

    I'm guessing some code paths are leaving the stream in undesired states, and doing a .Save accidentally resets it to a usable state.

     
  • Angel Marin

    Angel Marin - 2010-05-14

    I've commited a fix to svn that might be the cause to this. Can you give it a try?

     
  • Matt Drechsler

    Matt Drechsler - 2010-05-14

    Angel, the quick is response is much appreciated.

    Here is the code I am using to construct SharpMessage

        Private Sub InsertMessage(ByVal messageText As String)
            Dim message As New SharpMessage(messageText)

    I've had to give myself a crash course on building a dll in Visual Studio using your code, and after tracking down a few of the "gotchyas", most of which are already solved in this forum, I got one built and was able to test it.  I still get zeros.

    It is interesting being able to step through the C# code behind the DLL now in the Visual Studio debugger.  I can see that _stream is null, and so the code in SharpMessage.cs that checks for this then dumps into
        else if ( this._saved_file!=null )
            return this._saved_file.OpenRead();

    so that's why it works if I've saved the file.

    Am I missing something simple?  Why is _stream null?

    Feel free to call me a dunce and point out my silly mistake. :)

     
  • Angel Marin

    Angel Marin - 2010-05-14

    If this._stream is null once you've saved the file and that's ok, in-memory stream is closed once the file is already in filesystem. It's shouldn't be null before calling .Save and it's .Position should be 0 though.

    I haven't been able to reproduce this … what's the value of attachment.Stream.Position before calling Read (and without calling .Save)?

     
  • Matt Drechsler

    Matt Drechsler - 2010-05-14

    OK, I must have still had some debugging code in place from before I recompiled the DLL.
    All is working now!

    Just for completeness, to answer your question, the value of attachment.Stream.Position is 0 now before the Read, and equal to the Length immediately after.

    Is there any chance to get an updated binary posted?  I'm not sure I trust the kludgy hacks I had to perform to get the code to compile in VS. :)

    Thanks for the learning experience and again, your very quick response.

    -=Matt

     
  • Angel Marin

    Angel Marin - 2010-05-14

    Glad to hear it's working now.

    I'll try to get around to finally do a new binary release …

    BTW, you're better off using that svn compiled code than the last binary release, there has been like a zillion bug fixes since that release …

     

Log in to post a comment.

MongoDB Logo MongoDB