stilren / EpiForm-extensions

Episerver form extenstions: Multiple file upload, Send email with uploaded files as attachment, Linked dropdowns +more
MIT License
5 stars 2 forks source link

This only works for Epi Forms version 4.3.0 #2

Open mehdipanjwani opened 6 years ago

mehdipanjwani commented 6 years ago

This solution will only works for Epi Forms version 4.3.0. For EpiForms 4.8.0 it will attach incomplete files because EpiServer implemented security feature in EpiForms 4.7.0 onwards as per knowledge. Your solution tries to download the file again from contentassets folder which is now protected and requires login so your solution attaches html file.

The workaround is to read the input stream directly instead of downloading the uploaded file again. Read the input stream from this.HttpRequestContext.Files and attach the stream in the mail message.

Here is an idea how I did it:

public override object Run(object input)
        {
            /* The file uploading might take a while so we have to wait till Epi Forms successfully uploads the files.
            *  Then we can read the stream of uploaded files. task.Wait() is important here. Removing it will cause missing attachment problems. */
            Task task = Task.Factory.StartNew(() =>
            {
                GetUploadedFiles();
            });
            task.Wait();

            // This is inherited code of base class
            IEnumerable<EmailTemplateActorModel> emailModel = this.Model as IEnumerable<EmailTemplateActorModel>;
            if (emailModel == null || emailModel.Count<EmailTemplateActorModel>() < 1)
                return (object)null;

            this.SendMessage(emailModel);

            return (object)null;
        }
private IList<UploadedFile> GetUploadedFiles()
        {
            try
            {
                for (int i = 0; i < this.HttpRequestContext.Files.Count; i++)
                {
                    HttpPostedFileBase postedFile = this.HttpRequestContext.Files[i];
                    postedFile.InputStream.Position = 0;
                    postedFile.InputStream.Seek(0, SeekOrigin.Begin);

                    MemoryStream memoryStream = new MemoryStream();
                    postedFile.InputStream.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                    memoryStream.Seek(0, SeekOrigin.Begin);

                    uploadedFiles.Add(
                        new UploadedFile()
                        {
                            Name = postedFile.FileName,
                            Type = System.Web.MimeMapping.GetMimeMapping(postedFile.FileName),
                            InputStream = memoryStream
                        }
                    );
                }
            }
            catch (Exception ex)
            {
                PostSubmissionActorBase._logger.Error("Failed to get uploaded files: {0}", ex);
            }
            return uploadedFiles;
        }
public class UploadedFile
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public MemoryStream InputStream { get; set; }
    }
stilren commented 4 years ago

Hi! I think you guys have noticed but I am not maintaining this repo. I am not working with EpiServer anymore so keeping it up to date would be kind of hard.