evv_gis
evv_gis

Reputation: 104

Invalid Mail Attachment - Incorrect Path to Attachment

Here is my workflow:

Yes, there are a ton of posts regarding invalid mail attachments. However, I'm having trouble with the file path of the attachment and none of the posts I've read have been very helpful.

In my Mail Class, I create the attachment like so (note, data is another public class where File is a property):

oAttch = New MailAttachment(data.file.name)

The obvious problem is my attachment name is not the full path.

What I've done to get the full path:

I think the issue is I'm trying to get a full path of a file after it leaves the client. Perhaps there is no workaround or fix for this. Any suggestions would be great. If you need more information, please let me know.

EDIT

Following similar posts on the subject, I saw a reference to FileReader and added this to my click listener:

var file = $('#nuFile')[0].files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function () {
    data.file = reader.result;
    sendEmail(data);
}

The data.file created a base64 encoded string of the PDF which is later sent to my webservice (via AJAX in my sendMail function). However, now data.file in my webservice is type string.

I then created the attachment doing:

Dim ms As MemoryStream
If data.file IsNot Nothing Then
     ms = New MemoryStream(Encoding.UTF8.GetBytes(data.file))
     oMsg.Attachments.Add(New Attachment(ms, "NAF.pdf", "application/pdf"))
End If

The email sends with the attachment, but when I try to open the PDF, it says the file is corrupted. I used this page as a reference for creating the MemoryStream. Also, if the Base64 encoding string is too large, it surpasses the limits of JSON.

Perhaps it is obvious what I'm doing wrong, but this is a new world to me. Hope this helps!

Upvotes: 0

Views: 488

Answers (2)

trucker_jim
trucker_jim

Reputation: 572

Your .asmx could generate a GUID for the given file, and persist the file to the file system, database, session or wherever using the GUID as a key. The GUID is returned to the client.

Add an httphandler (ashx) into the solution which recieves the file GUID, accesses the file, converts it to a bytearray, then writes it to the response e.g.

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    context.Response.Clear()
    context.Response.ContentType = GetResponseContentType(FileExtension)
    context.Response.AddHeader("content-disposition", "attachment; filename=FileName.FileExtension")
    Dim btyearray As Byte()
    btyearray = YourFileAsAMemoryStream.ToArray
    context.Response.BinaryWrite(btyearray)
    YourFileAsAMemoryStream.Dispose()
    context.Response.Flush()
    context.Response.End()

End Sub

Public Function GetResponseContentType(ByVal fileExtension As String) As String

    Dim strExtension As String = fileExtension.ToLower
    strExtension = Replace(strExtension, ".", "")
    Select Case strExtension
        Case "bmp"
            Return "image/bmp"
        Case "jpg", "jpeg"
            Return "image/jpeg"
        Case "png"
            Return "image/png"
        Case "gif"
            Return "image/gif"
        Case "htm", "html"
            Return "text/HTML"
        Case "xml"
            Return "text/xml"
        Case "txt"
            Return "text/plain"
        Case "doc", "rtf", "docx"
            Return "application/msword"
        Case "csv", "xls"
            Return "Application/x-msexcel"
        Case "pdf"
            Return "application/pdf"
        Case Else
            Return "text/plain"
    End Select

End Function

That will allow you to do something like this:

<img src="myImageHandler.ashx?fileGuid=MyFileGUID" />

I know this is not a complete answer, and I don't have time to give a step by step fully working solution for this, but this gives a route if you are interested.

Upvotes: 1

Steve
Steve

Reputation: 5545

I believe you issue is in the fact that you are taking the path of the file on the client and sending it to the server which does not have the file in the same path. The best thing to do is not use the path of the file but to use one of the overloads of the Attachment and send it as a stream. I've done this before with PDF's and in a Winforms application but I don't know what issue you would run into with binary data in JSON.

Hope this at least gets you pointed to right way.

Upvotes: 0

Related Questions