开发者

Razor form - upload and attach to email

I am impleme开发者_JS百科nting a razor contact form on Umbraco CMS, which includes inline C# script to create and send an email using .Net's MailMessage. The end user now needs to be able to upload a file, which gets added to the email as an attachment.

From my knowledge, the only way to do this is by uploading to the disk and then loading the attachment from the disk.

Is it possible to upload and attach without writing to disk? Keep in mind there is no code-behind or controller - this is all in-line razor logic.


You need to pass the InputStream from the uploaded file to the MailMessage's Attachment constructor:

if(IsPost && Request.Files.Count > 0){
    var file = Request.Files[0];
    var fileName = file.FileName;
    var attachment = new Attachment(file.InputStream, fileName);       
    ...
    ...

Make sure that (unless you are using the FileUpload Helper) you set the form's method to POST, add an enctype attribute with a value of multipart/form-data, and that you provide your input type=file with a name attribute.


I'm not sure how much is possible with razor, but it should be possible to upload and attach to an email without saving to disk. Use the Stream you get from the FileUpload.FileContent property of the upload control. Then pass that to this constructor of the MailMessage Attachement.

Also, why not use code-behind?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜