Try our conversational search powered by Generative AI!

How to attach file to email by copying files from HttpPostedFileBase to MemoryStream?

Vote:
 

I'm using a custom SendEmailAfterSubmissionActor where i override the method Send. The reason if for attaching files to email when user is uploading files in my form.
CopyTo method in postFile.InputStream to MemoryStream i get this error.

StackTrace = " at System.IO.__Error.FileNotOpen()\r\n at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)\r\n at System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)\r\n at Test.Web.Extensions.EpiserverForms


Here is the full code for getting my files.
        private List<UploadedFile> GetUploadedFiles()
        {
            var uploadedFiles = new List<UploadedFile>();

            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 = MimeMapping.GetMimeMapping(postedFile.FileName),
                            InputStream = memoryStream
                        });
                }
            }
            catch (Exception ex)
            {
                PostSubmissionActorBase._logger.Error("Failed to get uploaded files: 
                     {0}", ex);
            }

            return uploadedFiles;
        }
#216299
Edited, Feb 03, 2020 8:58
Vote:
 

try to remove these lines:

postedFile.InputStream.Position = 0;
postedFile.InputStream.Seek(0, SeekOrigin.Begin);
#216314
Feb 03, 2020 18:50
Gonzalo Fuentes - Feb 04, 2020 12:34
thanks
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.