Here is my workflow:
- User selects local file while filling out HTML form
- AJAX sends the form data to my ASMX
- Public Classes take JSON data and use it for creating an email (with file being attachment
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!